<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/","images":"/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta property="og:type" content="website">
<meta property="og:title" content="一年春又来">
<meta property="og:url" content="http://littlefxc.github.io/page/22/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="一年春又来">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://littlefxc.github.io/page/22/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'zh-CN'
  };
</script>
<title>一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">234</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">38</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">125</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/07/Java%E5%B9%B6%E5%8F%91%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E4%B9%8Bvolatile/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2019/05/07/Java%E5%B9%B6%E5%8F%91%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E4%B9%8Bvolatile/" class="post-title-link" itemprop="url">Java并发学习记录之volatile</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-07 19:18:27" itemprop="dateCreated datePublished" datetime="2019-05-07T19:18:27+08:00">2019-05-07</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="Java并发学习记录之volatile"><a href="#Java并发学习记录之volatile" class="headerlink" title="Java并发学习记录之volatile"></a>Java并发学习记录之volatile</h1><p>首先来个感性认识：</p>
<ul>
<li><code>volatile</code> 关键字仅能实现对原始变量(如boolen、short、int、long等)赋值操作的原子性，<br>但是复合操作如 <code>i++</code>则不保证。</li>
</ul>
<h2 id="volatile-和-synchronized-的区别"><a href="#volatile-和-synchronized-的区别" class="headerlink" title="volatile 和 synchronized 的区别"></a>volatile 和 synchronized 的区别</h2><p><code>volatile</code> 关键字解决的是内存可见性的问题，会使得所有对volatile变量的读写都会直接刷到主存，<br>即保证了变量的可见性。<br>这样就能满足一些对变量可见性有要求而对读取顺序没有要求的需求。</p>
<p><code>synchronized</code> 关键字解决的是执行控制的问题，它会阻止其它线程获取当前对象的监控锁，<br>这样就使得当前对象中被synchronized关键字保护的代码块无法被其它线程访问，也就无法并发执行。<br>更重要的是，<code>synchronized</code> 还会创建一个内存屏障，内存屏障指令保证了所有CPU操作结果都会直接刷到主存中，<br>从而保证了操作的内存可见性，同时也使得先获得这个锁的线程的所有操作，<br>都 happens-before 于随后获得这个锁的线程的操作。</p>
<h3 id="区别点"><a href="#区别点" class="headerlink" title="区别点"></a>区别点</h3><ol>
<li><p><code>volatile</code> 本质是在告诉 JVM 当前变量在寄存器（工作内存）中的值是不确定的，需要从主存中读取；<br><code>synchronized</code> 则是锁定当前变量，只有当前线程可以访问该变量，其他线程被阻塞住。</p>
</li>
<li><p><code>volatile</code> 仅能使用在变量级别；<code>synchronized</code> 则可以使用在变量、方法、和类级别的。</p>
</li>
<li><p><code>volatile</code> 仅能实现变量的修改可见性，不能保证原子性；<br>而 <code>synchronized</code> 则可以保证变量的修改可见性和原子性。</p>
</li>
<li><p><code>volatile</code> 不会造成线程的阻塞；<code>synchronized</code> 可能会造成线程的阻塞。</p>
</li>
<li><p><code>volatile</code> 标记的变量不会被编译器优化；<code>synchronized</code> 标记的变量可以被编译器优化</p>
</li>
<li><p><code>volatile</code> 关键字用于解决变量在多个线程之间的可见性，而 <code>synchronized</code> 关键字解决的是多个线程之间访问资源的同步性。</p>
</li>
</ol>
<h2 id="volatile-的可见性"><a href="#volatile-的可见性" class="headerlink" title="volatile 的可见性"></a>volatile 的可见性</h2><p>下面是加了 <code>volatile</code> 关键字 和不加 <code>volatile</code> 关键字的区别，作图演示：</p>
<p><img src="https://img-blog.csdnimg.cn/20190226171404638.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>区别在于，volatile 修饰的成员变量在每次被线程访问时，<br>都强迫从主存（共享内存）中重读该成员变量的值。<br>而且，当成员变量发生变化时，强迫线程将变化值回写到主内存（共享内存）。<br>这样在任何时刻，两个不同的线程总是看到某个成员变量的同一个值，这样也就保证了同步数据的可见性。</p>
<h3 id="验证-volatile-的可见性"><a href="#验证-volatile-的可见性" class="headerlink" title="验证 volatile 的可见性"></a>验证 volatile 的可见性</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.base.thread;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/2/21</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">VolatileSample</span> <span class="keyword">extends</span> <span class="title">Thread</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> number;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">boolean</span> ready = <span class="keyword">true</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">while</span> (ready) &#123;</span><br><span class="line">            number++;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(ready);</span><br><span class="line">        System.out.println(number);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> InterruptedException </span>&#123;</span><br><span class="line">        Thread t1 = <span class="keyword">new</span> VolatileSample();</span><br><span class="line">        t1.start();</span><br><span class="line">        Thread.sleep(<span class="number">1000</span>);</span><br><span class="line">        ready = <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上面这段代码，我没有为 <code>ready</code> 变量添加 <code>volatile</code> 关键字，程序运行后陷入死循环。</p>
<p>为 <code>ready</code> 变量添加关键字 <code>volatile</code> 修饰后再次运行程序，及时停止，没有死循环。</p>
<p>验证结果：</p>
<p><img src="https://img-blog.csdnimg.cn/20190226171419692.png" alt="在这里插入图片描述"></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/07/Java%E5%B9%B6%E5%8F%91%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E4%B9%8B%E7%BA%BF%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2019/05/07/Java%E5%B9%B6%E5%8F%91%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E4%B9%8B%E7%BA%BF%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1/" class="post-title-link" itemprop="url">Java并发学习记录之线程间通信</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-07 19:16:43" itemprop="dateCreated datePublished" datetime="2019-05-07T19:16:43+08:00">2019-05-07</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="Java-并发学习记录之线程间通信"><a href="#Java-并发学习记录之线程间通信" class="headerlink" title="Java 并发学习记录之线程间通信"></a>Java 并发学习记录之线程间通信</h1><h2 id="volatile-和-synchronized-关键字"><a href="#volatile-和-synchronized-关键字" class="headerlink" title="volatile 和 synchronized 关键字"></a>volatile 和 synchronized 关键字</h2><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Little_fxc/article/details/87938990">Java并发学习记录之volatile</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Little_fxc/article/details/87861917">Java并发学习记录之synchronized</a></p>
<h2 id="等待-通知机制"><a href="#等待-通知机制" class="headerlink" title="等待/通知机制"></a>等待/通知机制</h2><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Little_fxc/article/details/87929728">Java并发学习记录之wait-notify机制</a></p>
<h2 id="管道输入-输出流"><a href="#管道输入-输出流" class="headerlink" title="管道输入/输出流"></a>管道输入/输出流</h2><p>管道输入/输出流和普通文件的输入/输出流或者网络输入、输出流不同之处在于管道输入/输出流主要用于线程之间的数据传输，<br>而且传输的媒介为内存。</p>
<p>管道输入/输出流主要包括下列两类的实现：</p>
<ul>
<li>面向字节： PipedOutputStream、 PipedInputStream</li>
<li>面向字符: PipedWriter、 PipedReader</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.base.thread.pipe;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.PipedReader;</span><br><span class="line"><span class="keyword">import</span> java.io.PipedWriter;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 管道输入/输出主要用于线程间的数据传输，传输的媒介是内存。具体实现：</span></span><br><span class="line"><span class="comment"> * &lt;br&gt;面向字节:</span></span><br><span class="line"><span class="comment"> * &lt;ul&gt;</span></span><br><span class="line"><span class="comment"> * &lt;li&gt;PipedWriter&lt;/li&gt;</span></span><br><span class="line"><span class="comment"> * &lt;li&gt;PipedReader&lt;/li&gt;</span></span><br><span class="line"><span class="comment"> * &lt;/ul&gt;</span></span><br><span class="line"><span class="comment"> * &lt;br&gt;面向字符:</span></span><br><span class="line"><span class="comment"> * &lt;ul&gt;</span></span><br><span class="line"><span class="comment"> * &lt;li&gt;PipedOutputStream&lt;/li&gt;</span></span><br><span class="line"><span class="comment"> * &lt;li&gt;PipedInputStream&lt;/li&gt;</span></span><br><span class="line"><span class="comment"> * &lt;/ul&gt;</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/2/26</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Piped</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        PipedWriter writer = <span class="keyword">new</span> PipedWriter();</span><br><span class="line">        PipedReader reader = <span class="keyword">new</span> PipedReader();</span><br><span class="line">        <span class="comment">// 将输出流和输入流进行必要的连接</span></span><br><span class="line">        writer.connect(reader);</span><br><span class="line">        Thread printThread = <span class="keyword">new</span> Thread(<span class="keyword">new</span> Print(reader), <span class="string">&quot;PrintThread&quot;</span>);</span><br><span class="line">        printThread.start();</span><br><span class="line">        <span class="keyword">int</span> receive = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">while</span> ((receive = System.in.read()) != -<span class="number">1</span>) &#123;</span><br><span class="line">                writer.write(receive);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            writer.close();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Print</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">private</span> PipedReader reader;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">Print</span><span class="params">(PipedReader reader)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.reader = reader;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">int</span> receive = <span class="number">0</span>;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="keyword">while</span> ((receive = reader.read()) != -<span class="number">1</span>) &#123;</span><br><span class="line">                    System.out.print((<span class="keyword">char</span>) receive);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行该示例，输入一组字符串，可以看到 printThread 进行了原样输出。</p>
<p><img src="https://img-blog.csdnimg.cn/20190226171159253.png" alt="在这里插入图片描述"></p>
<p>Tips：<br>对于 Piped 类型的流，必须要进行绑定，也就是调用 <code>connect()</code>，否则会抛异常。</p>
<h2 id="Thread-join-的使用"><a href="#Thread-join-的使用" class="headerlink" title="Thread.join() 的使用"></a>Thread.join() 的使用</h2><p>在很多情况下，主线程生成并起动了子线程，如果子线程里要进行大量的耗时的运算，<br>主线程往往将于子线程之前结束，但是如果主线程处理完其他的事务后，需要用到子线程的处理结果，<br>也就是主线程需要等待子线程执行完成之后再结束，这个时候就要用到join()方法了。另外，<br>一个线程需要等待另一个线程也需要用到join()方法。</p>
<p>Thread类除了提供 <code>join()</code> 方法之外，还提供了 <code>join(long millis)</code>、<code>join(long millis, int nanos)</code><br>两个具有超时特性的方法。这两个超时方法表示，如果线程thread在指定的超时时间没有终止，<br>那么将会从该超时方法中返回。</p>
<p>重点看一下 <code>join(long millis)</code> 的示例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.base.thread;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JoinLongTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            MyThread threadTest = <span class="keyword">new</span> MyThread();</span><br><span class="line">            threadTest.start();</span><br><span class="line"></span><br><span class="line">            threadTest.join(<span class="number">1000</span>);<span class="comment">// 主线成等待子线程1秒</span></span><br><span class="line"><span class="comment">//            Thread.sleep(1000);</span></span><br><span class="line">            System.out.println(<span class="string">&quot;主线程结束&quot;</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyThread</span> <span class="keyword">extends</span> <span class="title">Thread</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                Thread.sleep(<span class="number">2000</span>);</span><br><span class="line">                System.out.println(<span class="string">&quot;子线程结束&quot;</span>);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (InterruptedException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行结果：</p>
<p><img src="https://img-blog.csdnimg.cn/2019022617121653.png" alt="在这里插入图片描述"></p>
<h2 id="ThreadLocal"><a href="#ThreadLocal" class="headerlink" title="ThreadLocal"></a>ThreadLocal</h2><p>之前我们讲保证线程资源安全问题时，使用<strong>同步</strong>加锁的方式保证线程安全。还有一种办法就是<strong>隔离</strong>资源的做法。</p>
<p>所谓的<strong>隔离</strong>，即每个线程使用自己的局部资源。将资源隔离不让其它线程访问，<br>从被隔离资源的角度来说，能够访问它的只有当前线程。既然只有当前线程可以访问的数据，自然是线程安全的。<br>一个典型的例子就是 <code>Servlet</code>。</p>
<h3 id="ThreadLocal-的简单使用"><a href="#ThreadLocal-的简单使用" class="headerlink" title="ThreadLocal 的简单使用"></a>ThreadLocal 的简单使用</h3><p>首先来一个不使用 ThreadLocal 的类，然后在逐渐对其改造。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.base.thread.threadlocal;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.text.ParseException;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.ExecutorService;</span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.Executors;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/2/26</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ThreadLocal1</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> SimpleDateFormat SDF = <span class="keyword">new</span> SimpleDateFormat(<span class="string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        ExecutorService es = Executors.newFixedThreadPool(<span class="number">10</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">1000</span>; i++) &#123;</span><br><span class="line">            es.execute(<span class="keyword">new</span> ParseDate(i));</span><br><span class="line">        &#125;</span><br><span class="line">        es.shutdown();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">ParseDate</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">ParseDate</span><span class="params">(<span class="keyword">int</span> i)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.i = i;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">ParseDateWithSync</span><br><span class="line">                    Date date = SDF.parse(<span class="string">&quot;2019-02-26 16:23:&quot;</span> + i % <span class="number">60</span>);</span><br><span class="line">                    System.out.println(i + <span class="string">&quot;:&quot;</span> + date);</span><br><span class="line"><span class="comment">//                &#125;</span></span><br><span class="line">            &#125; <span class="keyword">catch</span> (ParseException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>运行结果：</p>
<p><img src="https://img-blog.csdnimg.cn/20190226171230594.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>一般这种问题主要是因为 SimpleDateFormat 在多线程环境下，是线程不安全的，所以如果你在多线程环境中共享了SimpleDateFormat的实例，<br>比如你在类似日期类中定义了一个全局的 SimpleDateFormat 对象，这样子肯定会出现上述的报错</p>
<p>一种解决办法就是加锁，在上面代码中可以将注释去掉后再次运行，也就不会出现这个问题了。</p>
<p>但是我现在要使用保存线程局部变量的ThreadLocal对象来保存每一个线程的SimpleDateFormat对象，针对上述代码做出改变：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.base.thread.threadlocal;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.text.ParseException;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.ExecutorService;</span><br><span class="line"><span class="keyword">import</span> java.util.concurrent.Executors;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/2/26</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ParseDateWithThreadLocal</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">final</span> String pattern = <span class="string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> ThreadLocal&lt;SimpleDateFormat&gt; threadLocal = <span class="keyword">new</span> ThreadLocal&lt;SimpleDateFormat&gt;();</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        ExecutorService es = Executors.newFixedThreadPool(<span class="number">10</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; <span class="number">1000</span>; i++) &#123;</span><br><span class="line">            es.execute(<span class="keyword">new</span> ParseDate(i));</span><br><span class="line">        &#125;</span><br><span class="line">        es.shutdown();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">ParseDate</span> <span class="keyword">implements</span> <span class="title">Runnable</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">ParseDate</span><span class="params">(<span class="keyword">int</span> i)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.i = i;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="comment">// 如果当前线程不持有 SimpleDateFormat 对象。那就新建并保存设置在当前线程中，如果已持有，则直接使用。</span></span><br><span class="line">                <span class="keyword">if</span> (threadLocal.get()==<span class="keyword">null</span>) &#123;</span><br><span class="line">                    threadLocal.set(<span class="keyword">new</span> SimpleDateFormat(pattern));</span><br><span class="line">                &#125;</span><br><span class="line">                Date date = threadLocal.get().parse(<span class="string">&quot;2019-02-26 16:23:&quot;</span> + i % <span class="number">60</span>);</span><br><span class="line">                System.out.println(i + <span class="string">&quot;:&quot;</span> + date);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (ParseException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意：从上面代码中也可以看出，为每一个线程分配一个对象的工作并不是由 ThreadLocal 来完成的，<br>而是需要开发人员在应用层面保证的。ThreadLocal 只是起到了一个容器的作用。</p>
<h3 id="ThreadLocal的原理"><a href="#ThreadLocal的原理" class="headerlink" title="ThreadLocal的原理"></a>ThreadLocal的原理</h3><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Little_fxc/article/details/87979410">ThreadLocal的原理</a></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/07/Java%E5%B9%B6%E5%8F%91%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E4%B9%8Bwait-notify%E6%9C%BA%E5%88%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2019/05/07/Java%E5%B9%B6%E5%8F%91%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E4%B9%8Bwait-notify%E6%9C%BA%E5%88%B6/" class="post-title-link" itemprop="url">Java并发学习记录之wait/notify机制</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-07 19:13:37" itemprop="dateCreated datePublished" datetime="2019-05-07T19:13:37+08:00">2019-05-07</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p>为了支持多线程之间的协作，JDK提供了两个非常重要的接口线程等待 <code>wait()</code> 方法和<br>通知 <code>notify()</code> 方法。</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2019/05/07/Java%E5%B9%B6%E5%8F%91%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E4%B9%8Bwait-notify%E6%9C%BA%E5%88%B6/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/07/Java%E5%B9%B6%E5%8F%91%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E4%B9%8Bsynchronized/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2019/05/07/Java%E5%B9%B6%E5%8F%91%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E4%B9%8Bsynchronized/" class="post-title-link" itemprop="url">Java并发学习记录之synchronized</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-07 19:12:05" itemprop="dateCreated datePublished" datetime="2019-05-07T19:12:05+08:00">2019-05-07</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="关键词-synchronized-的多种用法"><a href="#关键词-synchronized-的多种用法" class="headerlink" title="关键词 synchronized 的多种用法"></a>关键词 synchronized 的多种用法</h2><ul>
<li>指定加锁对象：对给定对象加锁，进入同步代码前要获得给定对象的锁</li>
<li>直接作用于实例方法：相当于对当前实例加锁，进入同步代码前要获得当前实例的锁</li>
<li>直接作用于静态方法：相当于对当前类加锁，进入同步代码前要获得当前类的锁</li>
</ul>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2019/05/07/Java%E5%B9%B6%E5%8F%91%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95%E4%B9%8Bsynchronized/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/06/MySQL%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1%E5%8E%9F%E7%90%86%E6%8E%A2%E7%A9%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2019/05/06/MySQL%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1%E5%8E%9F%E7%90%86%E6%8E%A2%E7%A9%B6/" class="post-title-link" itemprop="url">MySQL分布式事务原理探究</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-06 20:24:07" itemprop="dateCreated datePublished" datetime="2019-05-06T20:24:07+08:00">2019-05-06</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p>分布式事务处理的XA规范(由 AP、RM、TM 组成):</p>
<ul>
<li>其中应用程序（Application Program ，简称 AP）：AP 定义事务边界（定义事务开始和结束）并访问事务边界内的资源。</li>
<li>资源管理器（Resource Manager，简称 RM）：RM 管理计算机共享的资源，许多软件都可以去访问这些资源，资源包含比如数据库、文件系统、打印机服务器等。</li>
<li>事务管理器（Transaction Manager ，简称TM）：负责管理全局事务，分配事务唯一标识，监控事务的执行进度，并负责事务的提交、回滚、失败恢复等。</li>
</ul>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2019/05/06/MySQL%E5%88%86%E5%B8%83%E5%BC%8F%E4%BA%8B%E5%8A%A1%E5%8E%9F%E7%90%86%E6%8E%A2%E7%A9%B6/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/06/%E6%97%A5%E5%BF%97%E5%86%99%E5%85%A5%E6%95%B0%E6%8D%AE%E5%BA%93Logback-JDBCAppender/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2019/05/06/%E6%97%A5%E5%BF%97%E5%86%99%E5%85%A5%E6%95%B0%E6%8D%AE%E5%BA%93Logback-JDBCAppender/" class="post-title-link" itemprop="url">日志写入数据库Logback-JDBCAppender</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-06 20:21:39" itemprop="dateCreated datePublished" datetime="2019-05-06T20:21:39+08:00">2019-05-06</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:49" itemprop="dateModified" datetime="2021-03-25T21:15:49+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="Logback-DBAppender"><a href="#Logback-DBAppender" class="headerlink" title="Logback-DBAppender"></a>Logback-DBAppender</h1><p>DBAppender以独立于Java编程语言的格式将日志事件插入到三个数据库表中。<br>这三个表是 <code>logging_event</code>、<code>logging_event_property</code> 和 <code>logging_event_exception</code>。<br>在使用DBAppender之前，它们必须存在。Logback附带了创建表的SQL脚本。<br>它们可以在 <code>logback-classic/src/main/java/ch/qos/logback/classic/db/script</code> 文件夹下找到。<br>对于每个最流行的数据库系统，都有一个特定的脚本。如果您的特定类型的数据库系统缺少脚本，那么应该很容易以现有脚本为例编写一个脚本。<br>如果JDBC驱动程序支持JDBC 3.0规范中引入的getGeneratedKeys方法，假设您已经创建了如上所述的适当的数据库表，那么就不需要额外的步骤。<br>否则，必须有适合您的数据库系统的sql方言。目前，logback有H2、HSQL、MS SQL Server、MySQL、Oracle、PostgreSQL、SQLLite和Sybase等多种方言。</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2019/05/06/%E6%97%A5%E5%BF%97%E5%86%99%E5%85%A5%E6%95%B0%E6%8D%AE%E5%BA%93Logback-JDBCAppender/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/06/%E6%97%A5%E5%BF%97%E5%86%99%E5%85%A5%E6%95%B0%E6%8D%AE%E5%BA%93Log4j2-JDBCAppender/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2019/05/06/%E6%97%A5%E5%BF%97%E5%86%99%E5%85%A5%E6%95%B0%E6%8D%AE%E5%BA%93Log4j2-JDBCAppender/" class="post-title-link" itemprop="url">日志写入数据库Log4j2-JDBCAppender</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-06 20:16:04" itemprop="dateCreated datePublished" datetime="2019-05-06T20:16:04+08:00">2019-05-06</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:49" itemprop="dateModified" datetime="2021-03-25T21:15:49+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="Log4j2-JDBCAppender"><a href="#Log4j2-JDBCAppender" class="headerlink" title="Log4j2-JDBCAppender"></a>Log4j2-JDBCAppender</h1><p>JDBCAppender使用标准JDBC将日志事件写入关系数据库表。可以将其配置为使用JNDI数据源或自定义工厂方法获取JDBC连接。<br>无论采用哪种方法，都必须有连接池作为支持。否则，日志记录性能将受到很大影响。</p>
<h2 id="maven-依赖"><a href="#maven-依赖" class="headerlink" title="maven 依赖"></a>maven 依赖</h2><p>通过使用 <code>platform-bom</code> 来管理依赖的版本问题。使用 druid 作为 JDBCAppender 的连接池。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencyManagement</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.spring.platform<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>platform-bom<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>Cairo-RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">scope</span>&gt;</span>import<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">type</span>&gt;</span>pom<span class="tag">&lt;/<span class="name">type</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencyManagement</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.logging.log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j-slf4j-impl<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.logging.log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>druid<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.1.10<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="日志配置文件：log4j2-xml"><a href="#日志配置文件：log4j2-xml" class="headerlink" title="日志配置文件：log4j2.xml"></a>日志配置文件：log4j2.xml</h2><p><code>&lt;JDBC&gt;&lt;/JDBC&gt;</code> 是 Log4j2 的 JDBCAppender 的标签。</p>
<p><code>ConnectionFactory</code> 是一个实现 Druid 连接池的单例类， 通过 <code>method</code> 属性来获取 <code>java.sql.Connection</code>。</p>
<p><code>Column</code> 表示数据表 <code>log4j2</code> 的字段。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">Configuration</span> <span class="attr">status</span>=<span class="string">&quot;INFO&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">Appenders</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">Console</span> <span class="attr">name</span>=<span class="string">&quot;console&quot;</span> <span class="attr">target</span>=<span class="string">&quot;SYSTEM_OUT&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">PatternLayout</span> <span class="attr">pattern</span>=<span class="string">&quot;[%-5level] %d&#123;yyyy-MM-dd HH:mm:ss.SSS&#125; [%t] %c&#123;1&#125; - %msg%n&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">Console</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- JDBCAppender --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">JDBC</span> <span class="attr">name</span>=<span class="string">&quot;jdbc&quot;</span> <span class="attr">tableName</span>=<span class="string">&quot;log4j2&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">ConnectionFactory</span> <span class="attr">class</span>=<span class="string">&quot;com.littlefxc.examples.log4j2.ConnectionFactory&quot;</span> <span class="attr">method</span>=<span class="string">&quot;getDatabaseConnection&quot;</span>/&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- 数据表 log4j2 中的字段 --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">Column</span> <span class="attr">name</span>=<span class="string">&quot;time&quot;</span> <span class="attr">pattern</span>=<span class="string">&quot;%d&#123;yyyy-MM-dd HH:mm:ss.SSS&#125;&quot;</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">Column</span> <span class="attr">name</span>=<span class="string">&quot;level&quot;</span> <span class="attr">pattern</span>=<span class="string">&quot;%level&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">Column</span> <span class="attr">name</span>=<span class="string">&quot;logger&quot;</span> <span class="attr">pattern</span>=<span class="string">&quot;%logger&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">Column</span> <span class="attr">name</span>=<span class="string">&quot;message&quot;</span> <span class="attr">pattern</span>=<span class="string">&quot;%message&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">Column</span> <span class="attr">name</span>=<span class="string">&quot;exception&quot;</span> <span class="attr">pattern</span>=<span class="string">&quot;%ex&#123;full&#125;&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">JDBC</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">Appenders</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">Loggers</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">Logger</span> <span class="attr">name</span>=<span class="string">&quot;com.littlefxc.examples.log4j2&quot;</span> <span class="attr">level</span>=<span class="string">&quot;debug&quot;</span> <span class="attr">additivity</span>=<span class="string">&quot;false&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">AppenderRef</span> <span class="attr">ref</span>=<span class="string">&quot;jdbc&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">Logger</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">Root</span> <span class="attr">level</span>=<span class="string">&quot;debug&quot;</span> <span class="attr">additivity</span>=<span class="string">&quot;false&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">AppenderRef</span> <span class="attr">ref</span>=<span class="string">&quot;console&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">Root</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">Loggers</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">Configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="数据库文件：schema-sql"><a href="#数据库文件：schema-sql" class="headerlink" title="数据库文件：schema.sql"></a>数据库文件：schema.sql</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SET</span> NAMES utf8mb4;</span><br><span class="line"><span class="keyword">SET</span> FOREIGN_KEY_CHECKS <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> log4j2;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `log4j2`  (</span><br><span class="line">  `id` <span class="type">bigint</span>(<span class="number">20</span>) UNSIGNED <span class="keyword">NOT</span> <span class="keyword">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  `<span class="type">time</span>` <span class="type">char</span>(<span class="number">23</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8 <span class="keyword">COLLATE</span> utf8_general_ci <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">  `level` <span class="type">char</span>(<span class="number">5</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8 <span class="keyword">COLLATE</span> utf8_general_ci <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">  `logger` <span class="type">varchar</span>(<span class="number">255</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8 <span class="keyword">COLLATE</span> utf8_general_ci <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">  `message` <span class="type">varchar</span>(<span class="number">255</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8 <span class="keyword">COLLATE</span> utf8_general_ci <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">  `exception` <span class="type">varchar</span>(<span class="number">1000</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8 <span class="keyword">COLLATE</span> utf8_general_ci <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">  <span class="keyword">PRIMARY</span> KEY (`id`) <span class="keyword">USING</span> BTREE</span><br><span class="line">) ENGINE <span class="operator">=</span> MyISAM AUTO_INCREMENT <span class="operator">=</span> <span class="number">1</span> <span class="type">CHARACTER</span> <span class="keyword">SET</span> <span class="operator">=</span> utf8 <span class="keyword">COLLATE</span> <span class="operator">=</span> utf8_general_ci ROW_FORMAT <span class="operator">=</span> <span class="keyword">Dynamic</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">SET</span> FOREIGN_KEY_CHECKS <span class="operator">=</span> <span class="number">1</span>;</span><br></pre></td></tr></table></figure>

<h2 id="ConnectionFactory"><a href="#ConnectionFactory" class="headerlink" title="ConnectionFactory"></a>ConnectionFactory</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.log4j2;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.alibaba.druid.pool.DruidDataSource;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.sql.DataSource;</span><br><span class="line"><span class="keyword">import</span> java.io.File;</span><br><span class="line"><span class="keyword">import</span> java.io.FileInputStream;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.InputStream;</span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.SQLException;</span><br><span class="line"><span class="keyword">import</span> java.util.Properties;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Log4j2 ConnectionFactory</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ConnectionFactory</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> DataSource dataSource;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="title">ConnectionFactory</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Properties properties = <span class="keyword">new</span> Properties();</span><br><span class="line">        String lineSeparator = File.separator;</span><br><span class="line">        String fileName = String.join(lineSeparator,</span><br><span class="line">                System.getProperty(<span class="string">&quot;user.dir&quot;</span>), <span class="string">&quot;Log4j2-JDBCAppender&quot;</span>, <span class="string">&quot;src&quot;</span>, <span class="string">&quot;main&quot;</span>, <span class="string">&quot;resources&quot;</span>, <span class="string">&quot;db.properties&quot;</span>);</span><br><span class="line">        <span class="keyword">try</span> (InputStream stream = <span class="keyword">new</span> FileInputStream(fileName)) &#123;</span><br><span class="line">            properties.load(stream);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">this</span>.dataSource = <span class="keyword">new</span> DruidDataSource();</span><br><span class="line">        ((DruidDataSource) <span class="keyword">this</span>.dataSource).configFromPropety(properties);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Connection <span class="title">getDatabaseConnection</span><span class="params">()</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Singleton.INSTANCE.dataSource.getConnection();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">interface</span> <span class="title">Singleton</span> </span>&#123;</span><br><span class="line">        <span class="keyword">final</span> ConnectionFactory INSTANCE = <span class="keyword">new</span> ConnectionFactory();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>ConnectionFactory</code> 是一个实现 Druid 连接池的单例类。</p>
<h3 id="db-properties"><a href="#db-properties" class="headerlink" title="db.properties"></a>db.properties</h3><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">druid.url=jdbc:mysql://192.168.120.63:3306/learn?useSSL=false</span></span><br><span class="line"><span class="string">druid.username=root</span></span><br><span class="line"><span class="string">druid.password=123456</span></span><br><span class="line"><span class="string">druid.driverClassName=com.mysql.jdbc.Driver</span></span><br><span class="line"><span class="string">druid.maxActive=10</span></span><br><span class="line"><span class="string">druid.minIdle=5</span></span><br></pre></td></tr></table></figure>

<h2 id="启动"><a href="#启动" class="headerlink" title="启动"></a>启动</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.log4j2;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/2/11</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">App</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger log = LoggerFactory.getLogger(App.class);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        log.debug(<span class="string">&quot;This is debug&quot;</span>);</span><br><span class="line">        log.info(<span class="string">&quot;This is info&quot;</span>);</span><br><span class="line">        log.warn(<span class="string">&quot;This is warn&quot;</span>);</span><br><span class="line">        log.error(<span class="string">&quot;This is error&quot;</span>);</span><br><span class="line">        log.error(<span class="string">&quot;This is error&quot;</span>, <span class="keyword">new</span> RuntimeException(<span class="string">&quot;this is a exception&quot;</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/06/spring-expression%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2019/05/06/spring-expression%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/" class="post-title-link" itemprop="url">spring-expression学习记录</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-06 20:13:40" itemprop="dateCreated datePublished" datetime="2019-05-06T20:13:40+08:00">2019-05-06</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:49" itemprop="dateModified" datetime="2021-03-25T21:15:49+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="Spring-SpEl-表达式语言-学习记录"><a href="#Spring-SpEl-表达式语言-学习记录" class="headerlink" title="Spring SpEl 表达式语言 - 学习记录"></a>Spring SpEl 表达式语言 - 学习记录</h1><p>SpEL是类似于OGNL和JSF EL的表达式语言，能够在运行时构建复杂表达式，存取对象属性、对象方法调用等。<br>所有的SpEL都支持XML和Annotation两种方式，格式：<code>#&#123; expressoin &#125;</code></p>
<h2 id="Maven-依赖"><a href="#Maven-依赖" class="headerlink" title="Maven 依赖"></a>Maven 依赖</h2><p>因为 <code>spring-context</code> 依赖中包含 <code>spring-expression</code>，所以选择它。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-context<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h2><p>在后面的一系列的测试中，都有用到在 <code>@Before</code> 中的元素。我仅仅只是将一份这份单元测试类分解了而已。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SpelTest</span> </span>&#123;</span><br><span class="line">    ExpressionParser parser;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 测试 XML 配置 SpEl</span></span><br><span class="line">    User user;</span><br><span class="line"></span><br><span class="line">    BeanFactory beanFactory;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 测试注解配置 SpEl</span></span><br><span class="line">    FieldValueTestBean fieldValueTestBean;</span><br><span class="line"></span><br><span class="line">    MovieRecommender movieRecommender;</span><br><span class="line"></span><br><span class="line">    PropertyValueTestBean propertyValueTestBean;</span><br><span class="line"></span><br><span class="line">    StandardEvaluationContext context;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发明家列表</span></span><br><span class="line">    List&lt;Inventor&gt; inventorList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发明家特斯拉</span></span><br><span class="line">    Inventor inventorTesla;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发明家爱迪生</span></span><br><span class="line">    Inventor inventorEdlson;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发明家贝尔</span></span><br><span class="line">    Inventor inventorBell;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 发明家数组</span></span><br><span class="line">    Inventor[] inventorArrays;</span><br><span class="line"></span><br><span class="line">    Society society;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 初始化Spring</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Before</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUp</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        beanFactory = <span class="keyword">new</span> ClassPathXmlApplicationContext(<span class="string">&quot;classpath:spring-root.xml&quot;</span>);</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// 允许 SpEl 表达式访问 IOC 容器中的 bean</span></span><br><span class="line">            <span class="comment">// SpEL支持使用 &quot;@&quot; 符号来引用Bean, 在引用Bean时需要使用BeanResolver接口实现来查找Bean, Spring提供BeanFactoryResolver实现</span></span><br><span class="line">            context = <span class="keyword">new</span> StandardEvaluationContext();</span><br><span class="line">            context.setBeanResolver(<span class="keyword">new</span> BeanFactoryResolver(beanFactory));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// SpEl 解析器</span></span><br><span class="line">        parser = (ExpressionParser) beanFactory.getBean(<span class="string">&quot;parser&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// XML 配置 SpEl</span></span><br><span class="line">        user = (User) beanFactory.getBean(<span class="string">&quot;user&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 注解配置 SpEl</span></span><br><span class="line">        fieldValueTestBean = (FieldValueTestBean) beanFactory.getBean(<span class="string">&quot;fieldValueTestBean&quot;</span>);</span><br><span class="line">        movieRecommender = (MovieRecommender) beanFactory.getBean(<span class="string">&quot;movieRecommender&quot;</span>);</span><br><span class="line">        propertyValueTestBean = (PropertyValueTestBean) beanFactory.getBean(<span class="string">&quot;propertyValueTestBean&quot;</span>);</span><br><span class="line"></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// 设置 SpEl 的 根对象</span></span><br><span class="line">            inventorTesla = <span class="keyword">new</span> Inventor(<span class="string">&quot;尼古拉·特斯拉&quot;</span>, <span class="string">&quot;塞尔维亚裔美籍&quot;</span>);</span><br><span class="line">            inventorTesla.setPlaceOfBirth(<span class="keyword">new</span> PlaceOfBirth(<span class="string">&quot;利卡-塞尼县&quot;</span>, <span class="string">&quot;克罗地亚&quot;</span>));</span><br><span class="line"></span><br><span class="line">            inventorEdlson = <span class="keyword">new</span> Inventor(<span class="string">&quot;托马斯·阿尔瓦·爱迪生&quot;</span>, <span class="string">&quot;美国&quot;</span>);</span><br><span class="line">            inventorEdlson.setPlaceOfBirth(<span class="keyword">new</span> PlaceOfBirth(<span class="string">&quot;米兰&quot;</span>, <span class="string">&quot;美国俄亥俄州&quot;</span>));</span><br><span class="line"></span><br><span class="line">            inventorBell = <span class="keyword">new</span> Inventor(<span class="string">&quot;亚历山大·格拉汉姆·贝尔&quot;</span>, <span class="string">&quot;美国&quot;</span>);</span><br><span class="line">            inventorBell.setPlaceOfBirth(<span class="keyword">new</span> PlaceOfBirth(<span class="string">&quot;爱丁堡&quot;</span>, <span class="string">&quot;英国苏格兰&quot;</span>));</span><br><span class="line"></span><br><span class="line">            inventorList.add(inventorTesla);</span><br><span class="line">            inventorList.add(inventorEdlson);</span><br><span class="line">            inventorList.add(inventorBell);</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 数组和列表的内容通过使用方括号表示法获得</span></span><br><span class="line">            Inventor[] inventorArrays = <span class="keyword">new</span> Inventor[<span class="number">3</span>];</span><br><span class="line">            inventorArrays = inventorList.toArray(inventorArrays);</span><br><span class="line"></span><br><span class="line">            society = <span class="keyword">new</span> Society();</span><br><span class="line">            society.getOfficers().put(Society.President, inventorEdlson);</span><br><span class="line">            society.getOfficers().put(Society.Advisors, inventorList);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="1-入门"><a href="#1-入门" class="headerlink" title="1. 入门"></a>1. 入门</h2><h3 id="支持执行字符串表达式"><a href="#支持执行字符串表达式" class="headerlink" title="支持执行字符串表达式"></a>支持执行字符串表达式</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ExpressionParser parser = <span class="keyword">new</span> SpelExpressionParser();</span><br><span class="line">    Expression exp = parser.parseExpression(<span class="string">&quot;&#x27;Hello World&#x27;&quot;</span>);</span><br><span class="line">    String message = (String) exp.getValue();</span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;Hello World&quot;</span>, message);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="SpEL支持广泛的功能，例如调用方法，访问属性和调用构造函数"><a href="#SpEL支持广泛的功能，例如调用方法，访问属性和调用构造函数" class="headerlink" title="SpEL支持广泛的功能，例如调用方法，访问属性和调用构造函数"></a>SpEL支持广泛的功能，例如调用方法，访问属性和调用构造函数</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ExpressionParser parser = <span class="keyword">new</span> SpelExpressionParser();</span><br><span class="line">    <span class="comment">// 调用方法</span></span><br><span class="line">    Expression exp = parser.parseExpression(<span class="string">&quot;&#x27;Hello World&#x27;.concat(&#x27;!&#x27;)&quot;</span>);</span><br><span class="line">    String message = (String) exp.getValue();</span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;Hello World!&quot;</span>, message);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 调用属性</span></span><br><span class="line">    exp = parser.parseExpression(<span class="string">&quot;&#x27;Hello World&#x27;.bytes&quot;</span>);</span><br><span class="line">    <span class="keyword">byte</span>[] bytesE = <span class="string">&quot;Hello World&quot;</span>.getBytes();</span><br><span class="line">    <span class="keyword">byte</span>[] bytesA = (<span class="keyword">byte</span>[]) exp.getValue();</span><br><span class="line">    Assert.assertArrayEquals(bytesE, bytesA);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// SpEL还通过使用标准点表示法（例如prop1.prop2.prop3）和属性值的设置来支持嵌套属性。也可以访问公共字段。</span></span><br><span class="line">    exp = parser.parseExpression(<span class="string">&quot;&#x27;Hello World&#x27;.bytes.length&quot;</span>);</span><br><span class="line">    <span class="keyword">int</span> lengthE = <span class="string">&quot;Hello World&quot;</span>.getBytes().length;</span><br><span class="line">    <span class="keyword">int</span> lengthA = (<span class="keyword">int</span>) exp.getValue();</span><br><span class="line">    Assert.assertEquals(lengthE, lengthA);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 调用构造方法</span></span><br><span class="line">    exp = parser.parseExpression(<span class="string">&quot;new String(&#x27;hello world&#x27;).toUpperCase()&quot;</span>);</span><br><span class="line">    String constructorA = exp.getValue(String.class);</span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;HELLO WORLD&quot;</span>, constructorA);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="SpEL的更常见用法是提供针对特定对象实例（称为根对象）计算的表达式字符串"><a href="#SpEL的更常见用法是提供针对特定对象实例（称为根对象）计算的表达式字符串" class="headerlink" title="SpEL的更常见用法是提供针对特定对象实例（称为根对象）计算的表达式字符串"></a>SpEL的更常见用法是提供针对特定对象实例（称为根对象）计算的表达式字符串</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test3</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ExpressionParser parser = <span class="keyword">new</span> SpelExpressionParser();</span><br><span class="line"></span><br><span class="line">    Expression exp = parser.parseExpression(<span class="string">&quot;username == &#x27;fxc&#x27;&quot;</span>);</span><br><span class="line">    Boolean expValue = exp.getValue(user, Boolean.class);</span><br><span class="line">    Assert.assertEquals(<span class="keyword">true</span>, expValue);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Spring-Beans-中的-SpEl-表达式-Bean-的引用"><a href="#Spring-Beans-中的-SpEl-表达式-Bean-的引用" class="headerlink" title="Spring Beans 中的 SpEl 表达式, Bean 的引用"></a>Spring Beans 中的 SpEl 表达式, Bean 的引用</h3><p>Spring SpEL 可以在 XML 配置文件中使用，同样可以在注解配置中使用例如 <code>@Value(&quot;#&#123;expression&#125;&quot;)</code>。</p>
<p>Spring 允许 SpEl 表达式访问 IOC 容器中的 bean，SpEL支持使用 <code>@</code> 符号来引用Bean, 在引用 Bean 时需要使用 <code>BeanResolver</code> 接口实现来查找Bean, Spring提供 <code>BeanFactoryResolver</code> 实现</p>
<p>ClassPathXmlApplicationContext 实现默认会把 <code>System.getProperties()</code> 注册名为<code>systemProperties</code> 的 Bean，因此使用 <code>@systemProperties</code> 来引用该 Bean。</p>
<h4 id="在-Spring-配置文件中使用-SpEl-表达式"><a href="#在-Spring-配置文件中使用-SpEl-表达式" class="headerlink" title="在 Spring 配置文件中使用 SpEl 表达式"></a>在 Spring 配置文件中使用 SpEl 表达式</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:context</span>=<span class="string">&quot;http://www.springframework.org/schema/context&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:property-placeholder</span> <span class="attr">location</span>=<span class="string">&quot;classpath:application.properties&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.littlefxc.examples.spel&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;parser&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.expression.spel.standard.SpelExpressionParser&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 同样也适用于 Java Config, 例如 @Value(&quot;#&#123;systemProperties[&#x27;user.country&#x27;]&#125;&quot;) --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;user&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.littlefxc.examples.spel.User&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">value</span>=<span class="string">&quot;fxc&quot;</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--&lt;property name=&quot;uuid&quot; value=&quot;#&#123;T(java.util.UUID).randomUUID().toString()&#125;&quot;/&gt;--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;uuid&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;user.uuid&#125;&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;place&quot;</span> <span class="attr">value</span>=<span class="string">&quot;#&#123;systemProperties[&#x27;user.country&#x27;]&#125;&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;age&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;user.age&#125;&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>application.properties</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">user.uuid=#&#123;T(java.util.UUID).randomUUID().toString()&#125;</span></span><br><span class="line"><span class="string">user.age=18</span></span><br></pre></td></tr></table></figure>

<h4 id="在注解中用法1：在成员属性上使用-SpEL-表达式"><a href="#在注解中用法1：在成员属性上使用-SpEL-表达式" class="headerlink" title="在注解中用法1：在成员属性上使用 SpEL 表达式"></a>在注解中用法1：在成员属性上使用 SpEL 表达式</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.spel;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Value;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/1/24</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FieldValueTestBean</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value(&quot;#&#123; systemProperties[&#x27;user.country&#x27;] &#125;&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String defaultLocale;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 省略 setter getter</span></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="在注解中用法2：在构造方法上使用-SpEL-表达式"><a href="#在注解中用法2：在构造方法上使用-SpEL-表达式" class="headerlink" title="在注解中用法2：在构造方法上使用 SpEL 表达式"></a>在注解中用法2：在构造方法上使用 SpEL 表达式</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MovieRecommender</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String movieName;</span><br><span class="line">    <span class="keyword">private</span> String defaultLocale;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">MovieRecommender</span><span class="params">(<span class="meta">@Value(&quot;#&#123; systemProperties[&#x27;user.country&#x27;] &#125;&quot;)</span> String defaultLocale)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.defaultLocale = defaultLocale;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 省略 setter getter</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="在注解中用法3：在属性设置方法上使用-SpEL-表达式"><a href="#在注解中用法3：在属性设置方法上使用-SpEL-表达式" class="headerlink" title="在注解中用法3：在属性设置方法上使用 SpEL 表达式"></a>在注解中用法3：在属性设置方法上使用 SpEL 表达式</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.spel;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Value;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Component;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PropertyValueTestBean</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String defaultLocale;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getDefaultLocale</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.defaultLocale;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Value(&quot;#&#123; systemProperties[&#x27;user.country&#x27;] &#125;&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setDefaultLocale</span><span class="params">(String defaultLocale)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.defaultLocale = defaultLocale;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test4</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 允许 SpEl 表达式访问 IOC 容器中的 bean</span></span><br><span class="line">    <span class="comment">// SpEL支持使用 &quot;@&quot; 符号来引用Bean, 在引用Bean时需要使用BeanResolver接口实现来查找Bean, Spring提供BeanFactoryResolver实现</span></span><br><span class="line">    <span class="comment">// ClassPathXmlApplicationContext 实现默认会把&quot;System.getProperties()&quot;注册为&quot;systemProperties&quot;Bean，因此我们使用 &quot;@systemProperties&quot;来引用该Bean</span></span><br><span class="line">    Properties result1 = parser.parseExpression(<span class="string">&quot;@systemProperties&quot;</span>).getValue(context, Properties.class);</span><br><span class="line">    System.out.println(result1.getProperty(<span class="string">&quot;user.dir&quot;</span>, <span class="string">&quot;环境变量中没有该属性&quot;</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">// XML 配置</span></span><br><span class="line">    User userActual = parser.parseExpression(<span class="string">&quot;@user&quot;</span>).getValue(context, User.class);</span><br><span class="line">    Assert.assertSame(user, userActual);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 注解配置</span></span><br><span class="line">    Assert.assertSame(movieRecommender, parser.parseExpression(<span class="string">&quot;@movieRecommender&quot;</span>).getValue(context, MovieRecommender.class));</span><br><span class="line">    Assert.assertSame(propertyValueTestBean, parser.parseExpression(<span class="string">&quot;@propertyValueTestBean&quot;</span>).getValue(context, PropertyValueTestBean.class));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-字面量表达式"><a href="#2-字面量表达式" class="headerlink" title="2. 字面量表达式"></a>2. 字面量表达式</h2><p>SpEL支持的字面量包括：字符串、数字类型（int、long、float、double）、布尔类型、null类型。</p>
<table>
<thead>
<tr>
<th>类型</th>
<th>示例</th>
</tr>
</thead>
<tbody><tr>
<td>字符串</td>
<td>String str1 = parser.parseExpression(“‘Hello World!’”).getValue(String.class); <br> String str2 = parser.parseExpression(“\“Hello World!\“”).getValue(String.class);</td>
</tr>
<tr>
<td>数字类型</td>
<td>int int1 = parser.parseExpression(“1”).getValue(Integer.class); <br> long long1 = parser.parseExpression(“-1L”).getValue(long.class); <br> float float1 = parser.parseExpression(“1.1”).getValue(Float.class); <br> double double1 = parser.parseExpression(“1.1E+2”).getValue(double.class); <br> int hex1 = parser.parseExpression(“0xa”).getValue(Integer.class); <br> long hex2 = parser.parseExpression(“0xaL”).getValue(long.class);</td>
</tr>
<tr>
<td>布尔类型</td>
<td>boolean true1 = parser.parseExpression(“true”).getValue(boolean.class); <br> boolean false1 = parser.parseExpression(“false”).getValue(boolean.class);</td>
</tr>
<tr>
<td>null类型</td>
<td>Object null1 = parser.parseExpression(“null”).getValue(Object.class);</td>
</tr>
</tbody></table>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test5</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ExpressionParser parser = <span class="keyword">new</span> SpelExpressionParser();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// evals to &quot;Hello World&quot;</span></span><br><span class="line">    String helloWorld = (String) parser.parseExpression(<span class="string">&quot;&#x27;Hello World&#x27;&quot;</span>).getValue();</span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;Hello World&quot;</span>, helloWorld);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 浮点型字面量</span></span><br><span class="line">    <span class="keyword">double</span> avogadrosNumber = (Double) parser.parseExpression(<span class="string">&quot;6.0221415E+23&quot;</span>).getValue();</span><br><span class="line">    Assert.assertEquals(Double.valueOf(<span class="string">&quot;6.0221415E+23&quot;</span>), avogadrosNumber, Double.MIN_VALUE);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// evals to 2147483647</span></span><br><span class="line">    <span class="keyword">int</span> maxValue = (Integer) parser.parseExpression(<span class="string">&quot;0x7FFFFFFF&quot;</span>).getValue();</span><br><span class="line">    Assert.assertEquals(<span class="number">2147483647</span>, maxValue);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">boolean</span> trueValue = (Boolean) parser.parseExpression(<span class="string">&quot;true&quot;</span>).getValue();</span><br><span class="line">    Assert.assertTrue(trueValue);</span><br><span class="line"></span><br><span class="line">    Object nullValue = parser.parseExpression(<span class="string">&quot;null&quot;</span>).getValue();</span><br><span class="line">    Assert.assertNull(nullValue);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-集合的表达式-Properties-Arrays-Lists-Maps-and-Indexers"><a href="#3-集合的表达式-Properties-Arrays-Lists-Maps-and-Indexers" class="headerlink" title="3. 集合的表达式(Properties, Arrays, Lists, Maps, and Indexers)"></a>3. 集合的表达式(Properties, Arrays, Lists, Maps, and Indexers)</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 集合的表达式</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@see</span> java.util.Properties</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@see</span> java.util.Arrays</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@see</span> java.util.List</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@see</span> java.util.Map</span></span><br><span class="line"><span class="comment">     * Index 索引</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test6</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 属性名称的第一个字母不区分大小写</span></span><br><span class="line">        <span class="comment">// 数组和列表的内容通过使用方括号表示法获得</span></span><br><span class="line">        <span class="keyword">int</span> year = (Integer) parser.parseExpression(<span class="string">&quot;Birthdate.Year = 1856&quot;</span>).getValue(context);</span><br><span class="line">        String city = (String) parser.parseExpression(<span class="string">&quot;placeOfBirth.City&quot;</span>).getValue(context);</span><br><span class="line">        Assert.assertEquals(<span class="number">1856</span>, year);</span><br><span class="line">        Assert.assertEquals(<span class="string">&quot;利卡-塞尼县&quot;</span>, city);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 列表</span></span><br><span class="line">        context.setRootObject(inventorList);</span><br><span class="line">        String birthCity = parser</span><br><span class="line">                .parseExpression(<span class="string">&quot;[0].placeOfBirth.City&quot;</span>)</span><br><span class="line">                .getValue(context, String.class);</span><br><span class="line">        Assert.assertEquals(<span class="string">&quot;利卡-塞尼县&quot;</span>, birthCity);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 字典</span></span><br><span class="line">        context.setRootObject(society);</span><br><span class="line">        <span class="comment">// 改变字典的值的某个属性</span></span><br><span class="line">        parser.parseExpression(<span class="string">&quot;Officers[&#x27;president&#x27;].placeOfBirth.City&quot;</span>)</span><br><span class="line">                .setValue(context, <span class="string">&quot;随便&quot;</span>);</span><br><span class="line">        <span class="comment">// 获取字典的值的某个属性</span></span><br><span class="line">        birthCity = (String) parser</span><br><span class="line">                .parseExpression(<span class="string">&quot;officers[&#x27;president&#x27;].placeOfBirth.City&quot;</span>)</span><br><span class="line">                .getValue(context);</span><br><span class="line">        Assert.assertEquals(<span class="string">&quot;随便&quot;</span>, birthCity);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="4-内联列表-Inline-Lists"><a href="#4-内联列表-Inline-Lists" class="headerlink" title="4. 内联列表( Inline Lists )"></a>4. 内联列表( Inline Lists )</h2><p>可以使用{}符号直接在表达式中表示列表.{}本身表示一个空列表。出于性能原因，如果列表本身完全由固定的文字组成，那么将创建一个常量列表来表示表达式(而不是在每个表达式上构建一个新的列表)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 内联列表(Inline Lists)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test(expected = UnsupportedOperationException.class)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test7_1</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 将返回不可修改的空List</span></span><br><span class="line">    List list = parser.parseExpression(<span class="string">&quot;&#123;&#125;&quot;</span>).getValue(List.class);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 对于字面量列表也将返回不可修改的List</span></span><br><span class="line">    List list2 = parser.parseExpression(<span class="string">&quot;&#123;1,2,3,4&#125;&quot;</span>).getValue(List.class);</span><br><span class="line">    <span class="comment">// 不会进行不可修改处理</span></span><br><span class="line">    list2.set(<span class="number">0</span>, <span class="number">2</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 内联列表(Inline Lists)</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test7_2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 对于列表中只要有一个不是字面量表达式，将只返回原始List</span></span><br><span class="line">    List&lt;List&lt;Integer&gt;&gt; list = parser.parseExpression(<span class="string">&quot;&#123;&#123;1+2, 2+4&#125;,&#123;3, 4+4&#125;&#125;&quot;</span>).getValue(List.class);</span><br><span class="line">    <span class="comment">// 操作的不是原始列表</span></span><br><span class="line">    list.get(<span class="number">0</span>).set(<span class="number">0</span>, <span class="number">1</span>);</span><br><span class="line">    Integer value = parser.parseExpression(<span class="string">&quot;&#123;&#123;1+2, 2+4&#125;,&#123;3, 4+4&#125;&#125;[0][0]&quot;</span>).getValue(Integer.class);</span><br><span class="line">    Assert.assertEquals(<span class="number">1</span>, list.get(<span class="number">0</span>).get(<span class="number">0</span>).intValue());</span><br><span class="line">    Assert.assertEquals(<span class="number">3</span>, value.intValue());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="5-内联字典-Inline-Maps"><a href="#5-内联字典-Inline-Maps" class="headerlink" title="5. 内联字典( Inline Maps )"></a>5. 内联字典( Inline Maps )</h2><p>您可以使用熟悉的Java语法来构建数组，也可以提供一个初始化器来在构建时填充数组。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 内联字典(Inline Maps)</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="meta">@Test</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test8</span><span class="params">()</span> </span>&#123;</span><br><span class="line">       <span class="comment">// evaluates to a Java map containing the two entries</span></span><br><span class="line">       Map inventorInfo = (Map) parser.parseExpression(<span class="string">&quot;&#123;name:&#x27;Nikola&#x27;,dob:&#x27;10-July-1856&#x27;&#125;&quot;</span>).getValue(context);</span><br><span class="line">       Assert.assertEquals(<span class="string">&quot;Nikola&quot;</span>, inventorInfo.get(<span class="string">&quot;name&quot;</span>));</span><br><span class="line"></span><br><span class="line">       <span class="comment">// 字典中的字典</span></span><br><span class="line">       Map mapOfMaps = (Map) parser</span><br><span class="line">               .parseExpression(<span class="string">&quot;&#123;name:&#123;first:&#x27;Nikola&#x27;,last:&#x27;Tesla&#x27;&#125;,dob:&#123;day:10,month:&#x27;July&#x27;,year:1856&#125;&#125;&quot;</span>)</span><br><span class="line">               .getValue(context);</span><br><span class="line">       Map tesla = (Map) mapOfMaps.get(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">       Assert.assertEquals(<span class="string">&quot;Tesla&quot;</span>, tesla.get(<span class="string">&quot;last&quot;</span>));</span><br><span class="line"></span><br><span class="line">       <span class="comment">// 空字典 &#123;:&#125;</span></span><br><span class="line">       Map emptyMap = (Map) parser.parseExpression(<span class="string">&quot;&#123;:&#125;&quot;</span>).getValue(context);</span><br><span class="line">       Assert.assertEquals(<span class="number">0</span>, emptyMap.size());</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<p>{:}本身表示一个空映射。出于性能原因，如果映射本身由固定的文字或其他嵌套的常量结构(列表或映射)组成，则创建常量映射来表示表达式(而不是在每个表达式上构建新的映射)。</p>
<h2 id="6-数组构造-Array-Construction"><a href="#6-数组构造-Array-Construction" class="headerlink" title="6. 数组构造(Array Construction)"></a>6. 数组构造(Array Construction)</h2><p>您可以使用熟悉的Java语法来构建数组，也可以提供一个初始化器来在构建时填充数组</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 数组创建(Array Construction):相似的 Java 数组语法</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test9</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span>[] numbers1 = (<span class="keyword">int</span>[]) parser.parseExpression(<span class="string">&quot;new int[4]&quot;</span>).getValue(context);</span><br><span class="line">        Assert.assertArrayEquals(<span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>], numbers1);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Array with initializer</span></span><br><span class="line">        <span class="keyword">int</span>[] numbers2 = (<span class="keyword">int</span>[]) parser.parseExpression(<span class="string">&quot;new int[]&#123;1,2,3&#125;&quot;</span>).getValue(context);</span><br><span class="line">        Assert.assertArrayEquals(<span class="keyword">new</span> <span class="keyword">int</span>[]&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, numbers2);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Multi dimensional array</span></span><br><span class="line">        <span class="keyword">int</span>[][] numbers3 = (<span class="keyword">int</span>[][]) parser.parseExpression(<span class="string">&quot;new int[4][5]&quot;</span>).getValue(context);</span><br><span class="line">        Assert.assertArrayEquals(<span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">4</span>][<span class="number">5</span>], numbers3);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>在构造多维数组时，无法初始化，也就是 <code>&quot;new int[4][5]&quot;</code> 后面无法添加<code>&#123;&#125;</code></p>
<h2 id="8-方法调用"><a href="#8-方法调用" class="headerlink" title="8. 方法调用"></a>8. 方法调用</h2><p>您可以使用典型的Java编程语法来调用方法。您还可以对文字调用方法。还支持变量参数。下面的示例展示了如何调用方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 方法调用</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="meta">@Test</span></span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test10</span><span class="params">()</span> </span>&#123;</span><br><span class="line">       <span class="comment">// string literal, evaluates to &quot;bc&quot;</span></span><br><span class="line">       String bc = parser.parseExpression(<span class="string">&quot;&#x27;abc&#x27;.substring(1, 3)&quot;</span>).getValue(String.class);</span><br><span class="line">       Assert.assertEquals(<span class="string">&quot;bc&quot;</span>, bc);</span><br><span class="line"></span><br><span class="line">       society.getMembers().add(inventorTesla);</span><br><span class="line">       context.setRootObject(society);</span><br><span class="line">       <span class="comment">// evaluates to true</span></span><br><span class="line">       <span class="keyword">boolean</span> isMember = parser.parseExpression(<span class="string">&quot;isMember(&#x27;尼古拉·特斯拉&#x27;)&quot;</span>)</span><br><span class="line">               .getValue(context, Boolean.class);</span><br><span class="line">       Assert.assertTrue(isMember);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<h2 id="9-运算符"><a href="#9-运算符" class="headerlink" title="9. 运算符"></a>9. 运算符</h2><h3 id="9-1-关系运算符"><a href="#9-1-关系运算符" class="headerlink" title="9.1. 关系运算符"></a>9.1. 关系运算符</h3><p>关系运算符(等于、不等于、小于、小于或等于、大于、大于或等于)通过使用标准运算符表示法得到支持。下面的清单显示了一些操作符示例:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 运算符：关系运算符</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test11</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 关系运算符(等于、不等于、小于、小于或等于、大于、大于或等于、instanceof、正则表达式)</span></span><br><span class="line">        <span class="comment">// lt (&lt;) gt (&gt;) le (&lt;=) ge (&gt;=) eq (==) ne (!=) div (/) mod (%) not (!) 可以在XML文档中替换相应的符号</span></span><br><span class="line">        <span class="comment">// evaluates to true</span></span><br><span class="line">        <span class="keyword">boolean</span> flag = parser.parseExpression(<span class="string">&quot;2 == 2&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertTrue(flag);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to false</span></span><br><span class="line">        flag = parser.parseExpression(<span class="string">&quot;2 &lt; -5.0&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertFalse(flag);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to true</span></span><br><span class="line">        flag = parser.parseExpression(<span class="string">&quot;&#x27;black&#x27; &lt; &#x27;block&#x27;&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertTrue(flag);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to false</span></span><br><span class="line">        flag = parser.parseExpression(</span><br><span class="line">                <span class="string">&quot;&#x27;xyz&#x27; instanceof T(Integer)&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertFalse(flag);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to true</span></span><br><span class="line">        flag = parser.parseExpression(</span><br><span class="line">                <span class="string">&quot;&#x27;5.00&#x27; matches &#x27;^-?\\d+(\\.\\d&#123;2&#125;)?$&#x27;&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertTrue(flag);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//evaluates to false</span></span><br><span class="line">        flag = parser.parseExpression(</span><br><span class="line">                <span class="string">&quot;&#x27;5.0067&#x27; matches &#x27;^-?\\d+(\\.\\d&#123;2&#125;)?$&#x27;&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertFalse(flag);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>每个符号运算符也可以指定为纯字母等价的。这避免了使用的符号对嵌入表达式的文档类型具有特殊意义的问题(例如在XML文档中).</p>
<ul>
<li>lt (&lt;)</li>
<li>gt (&gt;)</li>
<li>le (&lt;=)</li>
<li>ge (&gt;=)</li>
<li>eq (==)</li>
<li>ne (!=)</li>
<li>div (/)</li>
<li>mod (%)</li>
<li>not (!)</li>
</ul>
<h3 id="9-2-逻辑运算符"><a href="#9-2-逻辑运算符" class="headerlink" title="9.2. 逻辑运算符"></a>9.2. 逻辑运算符</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 运算符：逻辑运算符</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test12</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        society.getMembers().add(inventorTesla);</span><br><span class="line">        society.getMembers().add(inventorEdlson);</span><br><span class="line">        context.setRootObject(society);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// -- AND --</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to false</span></span><br><span class="line">        <span class="keyword">boolean</span> flagAnd = parser.parseExpression(<span class="string">&quot;true and false&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertFalse(flagAnd);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to true</span></span><br><span class="line">        String expression = <span class="string">&quot;isMember(&#x27;尼古拉·特斯拉&#x27;) and isMember(&#x27;托马斯·阿尔瓦·爱迪生&#x27;)&quot;</span>;</span><br><span class="line">        flagAnd = parser.parseExpression(expression).getValue(context, Boolean.class);</span><br><span class="line">        Assert.assertTrue(flagAnd);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// -- OR --</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to true</span></span><br><span class="line">        <span class="keyword">boolean</span> flagOr = parser.parseExpression(<span class="string">&quot;true or false&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertTrue(flagOr);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to true</span></span><br><span class="line">        expression = <span class="string">&quot;isMember(&#x27;尼古拉·特斯拉&#x27;) or isMember(&#x27;托马斯·阿尔瓦·爱迪生&#x27;)&quot;</span>;</span><br><span class="line">        flagOr = parser.parseExpression(expression).getValue(context, Boolean.class);</span><br><span class="line">        Assert.assertTrue(flagOr);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// -- NOT --</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// evaluates to false</span></span><br><span class="line">        <span class="keyword">boolean</span> flagNot = parser.parseExpression(<span class="string">&quot;!true&quot;</span>).getValue(Boolean.class);</span><br><span class="line">        Assert.assertFalse(flagNot);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// -- AND and NOT --</span></span><br><span class="line">        expression = <span class="string">&quot;isMember(&#x27;尼古拉·特斯拉&#x27;) and !isMember(&#x27;托马斯·阿尔瓦·爱迪生&#x27;)&quot;</span>;</span><br><span class="line">        <span class="keyword">boolean</span> flagAndNot = parser.parseExpression(expression).getValue(context, Boolean.class);</span><br><span class="line">        Assert.assertFalse(flagAndNot);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="9-3-赋值运算符"><a href="#9-3-赋值运算符" class="headerlink" title="9.3. 赋值运算符"></a>9.3. 赋值运算符</h3><p>若要设置属性，请使用赋值运算符(=)。这通常在对setValue的调用中完成，但也可以在对getValue的调用中完成。下面的代码展示了使用赋值运算符的两种方法:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 运算符：赋值运算符</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test14</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    Inventor inventor = <span class="keyword">new</span> Inventor();</span><br><span class="line"></span><br><span class="line">    parser.parseExpression(<span class="string">&quot;Name&quot;</span>).setValue(context, inventor, <span class="string">&quot;Alexander Graham&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// alternatively</span></span><br><span class="line">    String bell = parser.parseExpression(</span><br><span class="line">        <span class="string">&quot;Name = &#x27;Alexander Graham Bell&#x27;&quot;</span>).getValue(context, inventor, String.class);</span><br><span class="line"></span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;Alexander Graham Bell&quot;</span>, bell);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="10-构造函数"><a href="#10-构造函数" class="headerlink" title="10. 构造函数"></a>10. 构造函数</h2><p>您可以通过使用新的操作符来调用构造函数。除了基本类型(int、float等)和字符串之外，应该对所有类型使用全限定类名。下面的例子展示了如何使用新的操作符来调用构造函数:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 类类型表达式：使用&quot;T(Type)&quot;来表示java.lang.Class实例，&quot;Type&quot;必须是类全限定名，&quot;java.lang&quot;包除外，即该包下的类可以不指定包名；</span></span><br><span class="line"><span class="comment">     * 使用类类型表达式还可以进行访问类静态方法及类静态字段。</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test15</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Inventor einstein = parser.parseExpression(</span><br><span class="line">                <span class="string">&quot;new com.littlefxc.examples.spel.Inventor(&#x27;爱因斯坦&#x27;, &#x27;德国&#x27;)&quot;</span>)</span><br><span class="line">                .getValue(Inventor.class);</span><br><span class="line">        Assert.assertEquals(<span class="keyword">new</span> Inventor(<span class="string">&quot;爱因斯坦&quot;</span>, <span class="string">&quot;德国&quot;</span>).getName(), einstein.getName());</span><br><span class="line"></span><br><span class="line">        context.setRootObject(society);</span><br><span class="line">        <span class="comment">//create new inventor instance within add method of List</span></span><br><span class="line">        parser.parseExpression(<span class="string">&quot;Members.add(new com.littlefxc.examples.spel.Inventor(&#x27;爱因斯坦&#x27;, &#x27;德国&#x27;))&quot;</span>).getValue(context);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="11-变量定义及引用"><a href="#11-变量定义及引用" class="headerlink" title="11. 变量定义及引用"></a>11. 变量定义及引用</h2><p>可以使用#variableName语法引用表达式中的变量。变量是通过在EvaluationContext实现上使用setVariable方法设置的。下面的例子展示了如何使用变量:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 变量定义及引用</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;</span></span><br><span class="line"><span class="comment">     * 变量定义通过 EvaluationContext 接口的 setVariable(variableName, value) 方法定义；&lt;br&gt;</span></span><br><span class="line"><span class="comment">     * 在表达式中使用 &quot;#variableName&quot; 引用；&lt;br&gt;</span></span><br><span class="line"><span class="comment">     * 除了引用自定义变量，SpEL还允许引用根对象及当前上下文对象，使用 &quot;#root&quot; 引用根对象，使用 &quot;#this&quot; 引用当前上下文对象；&lt;br&gt;</span></span><br><span class="line"><span class="comment">     * &lt;/p&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test16</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Inventor tesla = <span class="keyword">new</span> Inventor(<span class="string">&quot;Nikola Tesla&quot;</span>, <span class="string">&quot;Serbian&quot;</span>);</span><br><span class="line"></span><br><span class="line">        EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();</span><br><span class="line">        context.setVariable(<span class="string">&quot;newName&quot;</span>, <span class="string">&quot;Mike Tesla&quot;</span>);</span><br><span class="line"></span><br><span class="line">        parser.parseExpression(<span class="string">&quot;Name = #newName&quot;</span>).getValue(context, tesla);<span class="comment">// &quot;Mike Tesla&quot;</span></span><br><span class="line"><span class="comment">//        parser.parseExpression(&quot;#root.Name = #newName&quot;).getValue(context, tesla);// &quot;Mike Tesla&quot;</span></span><br><span class="line">        Assert.assertEquals(<span class="string">&quot;Mike Tesla&quot;</span>, tesla.getName());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// create an array of integers</span></span><br><span class="line">        List&lt;Integer&gt; primes = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        primes.addAll(Arrays.asList(<span class="number">2</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">11</span>, <span class="number">13</span>, <span class="number">17</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// create parser and set variable &#x27;primes&#x27; as the array of integers</span></span><br><span class="line">        context.setVariable(<span class="string">&quot;primes&quot;</span>, primes);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 通过使用诸如 (using selection ?&#123;...&#125;) 这样的选择表达式，选择列表中所有大于10的数字</span></span><br><span class="line">        <span class="comment">// evaluates to [11, 13, 17]</span></span><br><span class="line">        List&lt;Integer&gt; primesGreaterThanTen = (List&lt;Integer&gt;) parser</span><br><span class="line">                .parseExpression(<span class="string">&quot;#primes.?[#this&gt;10]&quot;</span>).getValue(context);</span><br><span class="line"></span><br><span class="line">        System.out.println(primesGreaterThanTen);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="12-自定义函数"><a href="#12-自定义函数" class="headerlink" title="12. 自定义函数"></a>12. 自定义函数</h2><p>您可以通过注册可在表达式字符串中调用的用户定义函数来扩展SpEL。 该功能通过EvaluationContext注册。 以下示例显示如何注册用户定义的函数：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 自定义函数</span></span><br><span class="line"><span class="comment">     * &lt;p&gt;</span></span><br><span class="line"><span class="comment">     * 目前只支持类静态方法注册为自定义函数；&lt;br&gt;</span></span><br><span class="line"><span class="comment">     * SpEL使用StandardEvaluationContext的registerFunction方法进行注册自定义函数，</span></span><br><span class="line"><span class="comment">     * 其实完全可以使用setVariable代替，两者其实本质是一样的；</span></span><br><span class="line"><span class="comment">     * &lt;/p&gt;</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test17</span><span class="params">()</span> <span class="keyword">throws</span> NoSuchMethodException </span>&#123;</span><br><span class="line">        Method parseInt = Integer.class.getDeclaredMethod(<span class="string">&quot;parseInt&quot;</span>, String.class);</span><br><span class="line">        <span class="comment">// 自定义函数推荐用 context.registerFunction(&quot;fnName&quot;, fn)</span></span><br><span class="line">        context.registerFunction(<span class="string">&quot;parseInt&quot;</span>, parseInt);</span><br><span class="line">        context.setVariable(<span class="string">&quot;parseInt2&quot;</span>, parseInt);</span><br><span class="line">        Boolean bool = parser.parseExpression(<span class="string">&quot;#parseInt(&#x27;3&#x27;) == #parseInt2(&#x27;3&#x27;)&quot;</span>).getValue(context, Boolean.class);</span><br><span class="line">        Assert.assertTrue(bool);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="13-三目运算及Elivis运算表达式"><a href="#13-三目运算及Elivis运算表达式" class="headerlink" title="13. 三目运算及Elivis运算表达式"></a>13. 三目运算及Elivis运算表达式</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 三目运算及Elivis运算表达式</span></span><br><span class="line"><span class="comment"> * &lt;br&gt;</span></span><br><span class="line"><span class="comment"> * 三目运算符 &quot;表达式1?表达式2:表达式3&quot;用于构造三目运算表达式，如&quot;2&gt;1?true:false&quot;将返回true；</span></span><br><span class="line"><span class="comment"> * &lt;br&gt;</span></span><br><span class="line"><span class="comment"> * Elivis运算符 &quot;表达式1?:表达式2&quot; 从Groovy语言引入用于简化三目运算符的，</span></span><br><span class="line"><span class="comment"> * 当 表达式1 为 非null 时则返回 表达式1，</span></span><br><span class="line"><span class="comment"> * 当 表达式1 为 null 时则返回 表达式2，</span></span><br><span class="line"><span class="comment"> * 简化了三目运算符方式 &quot;表达式1?表达式1:表达式2&quot;，如 &quot;null?:false&quot; 将返回 false，而 &quot;true?:false&quot; 将返回true；</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test18</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> int1 = parser.parseExpression(<span class="string">&quot;true and false ? 1 : 0&quot;</span>).getValue(Integer.class);</span><br><span class="line">    Assert.assertEquals(<span class="number">0</span>, int1);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 如果是 null, 返回 false</span></span><br><span class="line">    Boolean bool1 = parser.parseExpression(<span class="string">&quot;null ?: false&quot;</span>).getValue(Boolean.class);</span><br><span class="line">    Assert.assertFalse(bool1);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// true 不是 null, 返回 true</span></span><br><span class="line">    Boolean bool2 = parser.parseExpression(<span class="string">&quot;true ?: false&quot;</span>).getValue(Boolean.class);</span><br><span class="line">    Assert.assertTrue(bool2);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 稍微复杂点</span></span><br><span class="line">    Inventor tesla = <span class="keyword">new</span> Inventor(<span class="string">&quot;Nikola Tesla&quot;</span>, <span class="string">&quot;Serbian&quot;</span>);</span><br><span class="line">    String name = parser.parseExpression(<span class="string">&quot;Name?:&#x27;Elvis Presley&#x27;&quot;</span>).getValue(context, tesla, String.class);</span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;Nikola Tesla&quot;</span>, name);  <span class="comment">// Nikola Tesla</span></span><br><span class="line"></span><br><span class="line">    tesla.setName(<span class="keyword">null</span>);</span><br><span class="line">    name = parser.parseExpression(<span class="string">&quot;Name?:&#x27;Elvis Presley&#x27;&quot;</span>).getValue(context, tesla, String.class);</span><br><span class="line">    Assert.assertEquals(<span class="string">&quot;Elvis Presley&quot;</span>, name);  <span class="comment">// Elvis Presley</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 可以把Elivis运算表达式作为 默认值 例如 : @Value(&quot;#&#123;systemProperties[&#x27;pop3.port&#x27;] ?: 25&#125;&quot;)</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="14-安全的导航操作符"><a href="#14-安全的导航操作符" class="headerlink" title="14. 安全的导航操作符"></a>14. 安全的导航操作符</h2><p>安全导航操作符用于避免NullPointerException并来自Groovy语言。 通常，在引用对象时，可能需要在访问对象的方法或属性之前验证它是否为null。 为避免这种情况，安全导航操作符返回null而不是抛出异常。 以下示例显示如何使用安全导航运算符：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 安全的导航操作符：</span></span><br><span class="line"><span class="comment">     * 安全导航操作符用于避免NullPointerException，它来自Groovy语言。</span></span><br><span class="line"><span class="comment">     * 通常，当您有一个对象的引用时，您可能需要在访问该对象的方法或属性之前验证它是否为null。为了避免这种情况，安全导航操作符返回null，而不是抛出异常。</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test19</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Inventor tesla = <span class="keyword">new</span> Inventor(<span class="string">&quot;Nikola Tesla&quot;</span>, <span class="string">&quot;Serbian&quot;</span>);</span><br><span class="line">        tesla.setPlaceOfBirth(<span class="keyword">new</span> PlaceOfBirth(<span class="string">&quot;Smiljan&quot;</span>));</span><br><span class="line"></span><br><span class="line">        String city = parser.parseExpression(<span class="string">&quot;PlaceOfBirth?.City&quot;</span>).getValue(context, tesla, String.class);</span><br><span class="line">        Assert.assertEquals(<span class="string">&quot;Smiljan&quot;</span>, city);  <span class="comment">// Smiljan</span></span><br><span class="line"></span><br><span class="line">        tesla.setPlaceOfBirth(<span class="keyword">null</span>);</span><br><span class="line">        city = parser.parseExpression(<span class="string">&quot;PlaceOfBirth?.City&quot;</span>).getValue(context, tesla, String.class);</span><br><span class="line">        Assert.assertNull(city);  <span class="comment">// null - 没有抛出空指针异常(NullPointerException)!!!</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="15-集合选择-Collection-Selection"><a href="#15-集合选择-Collection-Selection" class="headerlink" title="15. 集合选择(Collection Selection)"></a>15. 集合选择(Collection Selection)</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 集合选择(Collection Selection)：</span></span><br><span class="line"><span class="comment">     * 选择表达式允许通过从源集合的条目中进行选择，将源集合转换为另一个集合。</span></span><br><span class="line"><span class="comment">     * 选择表达式通过使用形如 &quot;.?[selectionExpression]&quot; 的语法。它筛选集合并返回包含原始元素子集的新集合。</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test20</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        society.getMembers().addAll(inventorList);</span><br><span class="line">        context.setRootObject(society);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 从列表中选出国籍是美国的元素，然后组成新的列表</span></span><br><span class="line">        List&lt;Inventor&gt; list = (List&lt;Inventor&gt;) parser</span><br><span class="line">                .parseExpression(<span class="string">&quot;Members.?[Nationality == &#x27;塞尔维亚裔美籍&#x27;]&quot;</span>)</span><br><span class="line">                .getValue(context);</span><br><span class="line">        Assert.assertEquals(<span class="number">1</span>, list.size()); <span class="comment">// 只有特斯拉是塞尔维亚裔美籍</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 从原字典中选出值大于27的元素，然后组成新的字典</span></span><br><span class="line">        Map&lt;String, Integer&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">        map.put(<span class="string">&quot;a&quot;</span>, <span class="number">26</span>);</span><br><span class="line">        map.put(<span class="string">&quot;b&quot;</span>, <span class="number">27</span>);</span><br><span class="line">        map.put(<span class="string">&quot;c&quot;</span>, <span class="number">28</span>);</span><br><span class="line">        context.setVariable(<span class="string">&quot;map&quot;</span>, map);</span><br><span class="line">        Map newMap = (Map) parser.parseExpression(<span class="string">&quot;#map.?[value&lt;27]&quot;</span>).getValue(context);</span><br><span class="line">        Assert.assertEquals(<span class="number">1</span>, newMap.size());</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="16-集合投影-Collection-Projection"><a href="#16-集合投影-Collection-Projection" class="headerlink" title="16. 集合投影(Collection Projection)"></a>16. 集合投影(Collection Projection)</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 集合投影:</span></span><br><span class="line"><span class="comment">     * 投影让集合驱动子表达式的求值，结果是一个新的集合。</span></span><br><span class="line"><span class="comment">     * 投影的语法是 &quot;.![projectionExpression]&quot;。例如，假设我们有一个发明家列表，但是想要他们出生的城市列表。实际上，我们想要的是“出生地点”。</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test21</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        society.getMembers().addAll(inventorList);</span><br><span class="line">        context.setRootObject(society);</span><br><span class="line"></span><br><span class="line">        List&lt;String&gt; placesOfBirth = (List) parser.parseExpression(<span class="string">&quot;Members.![placeOfBirth.city]&quot;</span>).getValue(context);</span><br><span class="line">        MatcherAssert.assertThat(placesOfBirth, IsIterableContainingInOrder.contains(<span class="string">&quot;利卡-塞尼县&quot;</span>, <span class="string">&quot;米兰&quot;</span>, <span class="string">&quot;爱丁堡&quot;</span>));</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="17-引用"><a href="#17-引用" class="headerlink" title="17. 引用"></a>17. 引用</h2><p><a target="_blank" rel="noopener" href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions">Spring Expression Language (SpEL)</a></p>
<p><a target="_blank" rel="noopener" href="https://jinnianshilongnian.iteye.com/blog/1418309">https://jinnianshilongnian.iteye.com/blog/1418309</a></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/06/spring-batch%E5%85%A5%E9%97%A8%E4%B9%8BCSV-to-DB/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2019/05/06/spring-batch%E5%85%A5%E9%97%A8%E4%B9%8BCSV-to-DB/" class="post-title-link" itemprop="url">spring-batch入门之CSV-to-DB</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-06 20:11:49" itemprop="dateCreated datePublished" datetime="2019-05-06T20:11:49+08:00">2019-05-06</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:49" itemprop="dateModified" datetime="2021-03-25T21:15:49+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p>学习使用Spring batch从CSV文件读取记录，并使用 <code>StaxEventItemWriter</code> 输出经过处理的记录转换为 XML 的数据。</p>
<p><img src="https://img-blog.csdnimg.cn/20190131143346968.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70" alt="spring-batch-reference-model.png"></p>
<ol>
<li><p>JobLauncher： 顾名思义，该领域对象就是Job的启动器，其作用就是绑定一组JobParameters到Job上，然后运行该Job。</p>
</li>
<li><p>Job： 定义，配置批处理任务的领域对象，该对象的作用，第一是做Step的容器，配置该批处理任务需要的Step，以及他们之间的逻辑关系。第二是配置该批处理任务的特征，比方说名字，是否可重启，是否对JobParameters进行验证以及验证规则等。</p>
</li>
<li><p>Step： 定义批处理任务中一个对立的逻辑任务处理单元。基本上的业务逻辑处理代码都是封装在Step中的。Step有2种实现形式，一种是Tasklet形式的，这种形式非常自由，开发人员只需要实现Tasklet接口，其中的逻辑完全有自己决定，另一种是Chunk-Oriented形式的，这种形式定义了一个Step的流程必须是“读-处理（可选）-写”，当然Spring Batch也对每一个步骤提供了接口ItemReader， ItemProcessor，ItemWriter还有很多常用的默认实现（读文件，读数据库，写文件，写数据库等等）。 每一个Step只能由一个Tasklet或者一个Chunk构成。</p>
</li>
<li><p>JobRepository： 该领域对象会为Spring Batch的运维数据提供一种持久化机制。其为所有的运维数据的提供CRUD的操作接口，并为所有的操作提供事务支持。</p>
</li>
</ol>
<h2 id="项目概述"><a href="#项目概述" class="headerlink" title="项目概述"></a>项目概述</h2><p>在这个应用程序中，我们将执行以下任务:</p>
<ol>
<li>使用 <code>FlatFileItemReader</code> 从CSV文件读取交易记录</li>
<li>使用 <code>CustomItemProcessor</code> 进行项目的业务处理。当 <code>ItemReader</code> 读取一个项目，而 <code>ItemWriter</code> 写入它们时，<br><code>ItemProcessor</code> 提供一个转换或应用其他业务处理的访问点。 </li>
<li><del>使用 <code>StaxEventItemWriter</code> 获取 <code>CustomItemProcessor</code> 的处理结果，并将它转换成 XML 类型数据作为最终输出。</del></li>
<li>使用 <code>MyBatisBatchItemWriter</code> 获取 <code>CustomItemProcessor</code> 的处理结果，并将它转换成 XML 类型数据作为最终输出。</li>
<li>查看MySQL</li>
</ol>
<h2 id="工程结构"><a href="#工程结构" class="headerlink" title="工程结构"></a>工程结构</h2><p><img src="https://img-blog.csdnimg.cn/20190131143512507.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xpdHRsZV9meGM=,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h2 id="Maven-依赖"><a href="#Maven-依赖" class="headerlink" title="Maven 依赖"></a>Maven 依赖</h2><p><code>sqlite-jdbc</code> 和 <code>mysql-connector-java</code> 可以选择其中一个。<br>当选择其中一种时，同时也要在 <code>applicationContext.xml</code> 文件中做出相应的改动。</p>
<p>改动:</p>
<ul>
<li>依赖的版本由 <code>platform-bom</code> 来统一管理</li>
<li>添加 <code>mybatis</code>, <code>mybatis-spring</code></li>
</ul>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span></span><br><span class="line"><span class="tag">         <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">         <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.littlefxc.example<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>Spring-CSV-to-DB<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0-snapshot<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">project.build.sourceEncoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">project.build.sourceEncoding</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">spring.version</span>&gt;</span>5.0.9.RELEASE<span class="tag">&lt;/<span class="name">spring.version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">spring.batch.version</span>&gt;</span>4.0.1.RELEASE<span class="tag">&lt;/<span class="name">spring.batch.version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">sqlite.version</span>&gt;</span>3.8.11.2<span class="tag">&lt;/<span class="name">sqlite.version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">mysql.version</span>&gt;</span>5.1.47<span class="tag">&lt;/<span class="name">mysql.version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependencyManagement</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.spring.platform<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>platform-bom<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">version</span>&gt;</span>Cairo-RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">type</span>&gt;</span>pom<span class="tag">&lt;/<span class="name">type</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">scope</span>&gt;</span>import<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependencyManagement</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- MySQL database driver --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- spring的XML文件处理依赖 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-oxm<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.5.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-spring<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-jdbc<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.batch<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-batch-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.projectlombok<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lombok<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="applicationContext-xml"><a href="#applicationContext-xml" class="headerlink" title="applicationContext.xml"></a>applicationContext.xml</h2><p>我们将使用 <code>FlatFileItemReader</code> 读取 CSV 文件。<br>我们将使用它的标准配置，包括 <code>DefaultLineMapper</code>，<code>DelimitedLineTokenizer</code> 和 <code>BeanWrapperFieldSetMapper</code> 类。<br>为了在XML文件中输出记录，我们将使用 <code>StaxEventItemWriter</code> 作为标准编写器。</p>
<p>改动：</p>
<ul>
<li>将输出XML变为输出到mysql</li>
<li>Spring Batch 持久层框架由 <code>spring-jdbc</code> 改为 <code>mybatis</code>, <code>mybatis-spring</code> </li>
</ul>
<p>当然，原来的输出 <code>itemWriter</code> 去掉注释后，仍然起作用</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.springframework.org/schema/beans&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:jdbc</span>=<span class="string">&quot;http://www.springframework.org/schema/jdbc&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:batch</span>=<span class="string">&quot;http://www.springframework.org/schema/batch&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:p</span>=<span class="string">&quot;http://www.springframework.org/schema/p&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:context</span>=<span class="string">&quot;http://www.springframework.org/schema/context&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:tx</span>=<span class="string">&quot;http://www.springframework.org/schema/tx&quot;</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;</span></span></span><br><span class="line"><span class="tag"><span class="string">       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">       http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:property-placeholder</span> <span class="attr">location</span>=<span class="string">&quot;classpath:application.properties&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.littlefxc.examples.batch&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 1. 数据库脚本：这里是为了方便起见保证每次重启程序数据库都是最新的（生产环境中不要这么做!!!） --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">jdbc:initialize-database</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">jdbc:script</span> <span class="attr">location</span>=<span class="string">&quot;$&#123;batch.schema-drop&#125;&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">jdbc:script</span> <span class="attr">location</span>=<span class="string">&quot;$&#123;batch.schema-create&#125;&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">jdbc:script</span> <span class="attr">location</span>=<span class="string">&quot;$&#123;project.schema-drop&#125;&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">jdbc:script</span> <span class="attr">location</span>=<span class="string">&quot;$&#123;project.schema-create&#125;&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">jdbc:initialize-database</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 2. 连接数据库：可以选择其它的数据源实现 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:driverClassName</span>=<span class="string">&quot;$&#123;jdbc.driver-class-name&#125;&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:url</span>=<span class="string">&quot;$&#123;jdbc.url&#125;&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:username</span>=<span class="string">&quot;$&#123;jdbc.username&#125;&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:password</span>=<span class="string">&quot;$&#123;jdbc.password&#125;&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 3. 事务管理 --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.batch.support.transactionRecord.ResourcelessTransactionManager&quot;/&gt;--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;transactionManager&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:dataSource-ref</span>=<span class="string">&quot;dataSource&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 3.1 申明式事务 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tx:annotation-driven</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 3.2 mybatis 配置 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;sqlSessionFactory&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.SqlSessionFactoryBean&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:dataSource-ref</span>=<span class="string">&quot;dataSource&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:typeAliasesPackage</span>=<span class="string">&quot;$&#123;mybatis.type-aliases-package&#125;&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:configLocation</span>=<span class="string">&quot;$&#123;mybatis.configuration&#125;&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 3.3 mybatis dao 配置 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.mapper.MapperScannerConfigurer&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:basePackage</span>=<span class="string">&quot;com.littlefxc.examples.batch.dao&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:sqlSessionFactoryBeanName</span>=<span class="string">&quot;sqlSessionFactory&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 4. 为JobLauncher，Job和Step实现提供CRUD操作 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;jobRepository&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.batch.core.repository.support.JobRepositoryFactoryBean&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:dataSource-ref</span>=<span class="string">&quot;dataSource&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:transactionManager-ref</span>=<span class="string">&quot;transactionManager&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:databaseType</span>=<span class="string">&quot;mysql&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 5. JobLauncher表示一个简单的接口，用于使用给定的 JobParameter 启动作业 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;jobLauncher&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.batch.core.launch.support.SimpleJobLauncher&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">p:jobRepository-ref</span>=<span class="string">&quot;jobRepository&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 6. batch 输入 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;itemReader&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.batch.item.file.FlatFileItemReader&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 输入资源 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;resource&quot;</span> <span class="attr">value</span>=<span class="string">&quot;input/record.csv&quot;</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 不读取第一行数据 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;linesToSkip&quot;</span> <span class="attr">value</span>=<span class="string">&quot;1&quot;</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 将输入资源转化为对象 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;lineMapper&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.batch.item.file.mapping.DefaultLineMapper&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;lineTokenizer&quot;</span>&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.batch.item.file.transform.DelimitedLineTokenizer&quot;</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;names&quot;</span> <span class="attr">value</span>=<span class="string">&quot;username,user_id,transaction_date,transaction_amount&quot;</span>/&gt;</span></span><br><span class="line">                    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;fieldSetMapper&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;recordFieldSetMapper&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 7. batch的处理器 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;itemProcessor&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.littlefxc.examples.batch.service.CustomItemProcessor&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 8. batch 输出:输出为xml --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;bean id=&quot;itemWriter&quot; class=&quot;org.springframework.batch.item.xml.StaxEventItemWriter&quot;&gt;</span></span><br><span class="line"><span class="comment">        &lt;property name=&quot;resource&quot; value=&quot;file:xml/output.xml&quot;/&gt;</span></span><br><span class="line"><span class="comment">        &lt;property name=&quot;marshaller&quot;&gt;</span></span><br><span class="line"><span class="comment">            &amp;lt;!&amp;ndash; xml 与 对象的转换器 &amp;ndash;&amp;gt;</span></span><br><span class="line"><span class="comment">            &lt;bean class=&quot;org.springframework.oxm.jaxb.Jaxb2Marshaller&quot;</span></span><br><span class="line"><span class="comment">                  p:packagesToScan=&quot;com.littlefxc.examples.batch.model&quot;/&gt;</span></span><br><span class="line"><span class="comment">        &lt;/property&gt;</span></span><br><span class="line"><span class="comment">        &lt;property name=&quot;rootTagName&quot; value=&quot;transactionRecord&quot;/&gt;</span></span><br><span class="line"><span class="comment">    &lt;/bean&gt;--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 8. batch 输出:输出到mysql --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;bean id=&quot;itemWriter&quot; class=&quot;org.springframework.batch.item.database.JdbcBatchItemWriter&quot;</span></span><br><span class="line"><span class="comment">          p:dataSource-ref=&quot;dataSource&quot; p:sql=&quot;$&#123;batch.sql&#125;&quot;&gt;</span></span><br><span class="line"><span class="comment">        &lt;property name=&quot;itemSqlParameterSourceProvider&quot;&gt;</span></span><br><span class="line"><span class="comment">            &lt;bean class=&quot;org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider&quot;/&gt;</span></span><br><span class="line"><span class="comment">        &lt;/property&gt;</span></span><br><span class="line"><span class="comment">    &lt;/bean&gt;--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 8. batch 输出:使用 mybatis 输出到mysql --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;itemWriter&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.batch.MyBatisBatchItemWriter&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;sqlSessionFactory&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;sqlSessionFactory&quot;</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 与 mybatis 关联的 dao 层的接口名：com.littlefxc.examples.batch.dao.TransactionRecordDao.insertTransactionRecord --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;statementId&quot;</span> <span class="attr">value</span>=<span class="string">&quot;insertTransactionRecord&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 9. 配置batch的输入(6)、处理器(7)、输出(8) --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- commit-interval:提交事务之前将处理的项目数。 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">batch:job</span> <span class="attr">id</span>=<span class="string">&quot;firstBatchJob&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">batch:step</span> <span class="attr">id</span>=<span class="string">&quot;step1&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">batch:tasklet</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">batch:chunk</span> <span class="attr">reader</span>=<span class="string">&quot;itemReader&quot;</span> <span class="attr">processor</span>=<span class="string">&quot;itemProcessor&quot;</span> <span class="attr">writer</span>=<span class="string">&quot;itemWriter&quot;</span> <span class="attr">commit-interval</span>=<span class="string">&quot;2&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">batch:tasklet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">batch:step</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">batch:job</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="RecordFieldSetMapper"><a href="#RecordFieldSetMapper" class="headerlink" title="RecordFieldSetMapper"></a>RecordFieldSetMapper</h2><p><code>ItemReader</code> 的属性，作用是将 <code>FieldSet</code> 转换为对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.batch.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.littlefxc.examples.batch.model.TransactionRecord;</span><br><span class="line"><span class="keyword">import</span> org.springframework.batch.item.file.mapping.FieldSetMapper;</span><br><span class="line"><span class="keyword">import</span> org.springframework.batch.item.file.transform.FieldSet;</span><br><span class="line"><span class="keyword">import</span> org.springframework.validation.BindException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.text.ParseException;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 将 FieldSet 转换为对象</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/1/4</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RecordFieldSetMapper</span> <span class="keyword">implements</span> <span class="title">FieldSetMapper</span>&lt;<span class="title">Transaction</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Transaction <span class="title">mapFieldSet</span><span class="params">(FieldSet fieldSet)</span> <span class="keyword">throws</span> BindException </span>&#123;</span><br><span class="line">        SimpleDateFormat dateFormat = <span class="keyword">new</span> SimpleDateFormat(<span class="string">&quot;dd/MM/yyyy&quot;</span>);</span><br><span class="line">        Transaction transactionRecord = <span class="keyword">new</span> Transaction();</span><br><span class="line"></span><br><span class="line">        transactionRecord.setUsername(fieldSet.readString(<span class="string">&quot;username&quot;</span>));</span><br><span class="line">        transactionRecord.setUserId(fieldSet.readInt(<span class="string">&quot;user_id&quot;</span>));</span><br><span class="line">        transactionRecord.setAmount(fieldSet.readDouble(<span class="string">&quot;transaction_amount&quot;</span>));</span><br><span class="line">        String dateString = fieldSet.readString(<span class="string">&quot;transaction_date&quot;</span>);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            transactionRecord.setTransactionDate(dateFormat.parse(dateString));</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ParseException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> transactionRecord;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="CustomItemProcessor"><a href="#CustomItemProcessor" class="headerlink" title="CustomItemProcessor"></a>CustomItemProcessor</h2><p>自定义实现接口 <code>ItemProcessor</code>, 作为 <code>ItemReader</code> 和 <code>ItemWriter</code> 的转换点。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.batch.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.littlefxc.examples.batch.model.TransactionRecord;</span><br><span class="line"><span class="keyword">import</span> org.springframework.batch.item.file.mapping.FieldSetMapper;</span><br><span class="line"><span class="keyword">import</span> org.springframework.batch.item.file.transform.FieldSet;</span><br><span class="line"><span class="keyword">import</span> org.springframework.validation.BindException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.text.ParseException;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 将读取到的数据集合转换为对象</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span> 2019/1/4</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RecordFieldSetMapper</span> <span class="keyword">implements</span> <span class="title">FieldSetMapper</span>&lt;<span class="title">Transaction</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Transaction <span class="title">mapFieldSet</span><span class="params">(FieldSet fieldSet)</span> <span class="keyword">throws</span> BindException </span>&#123;</span><br><span class="line">        SimpleDateFormat dateFormat = <span class="keyword">new</span> SimpleDateFormat(<span class="string">&quot;dd/MM/yyyy&quot;</span>);</span><br><span class="line">        Transaction transactionRecord = <span class="keyword">new</span> Transaction();</span><br><span class="line"></span><br><span class="line">        transactionRecord.setUsername(fieldSet.readString(<span class="string">&quot;username&quot;</span>));</span><br><span class="line">        transactionRecord.setUserId(fieldSet.readInt(<span class="string">&quot;user_id&quot;</span>));</span><br><span class="line">        transactionRecord.setAmount(fieldSet.readDouble(<span class="string">&quot;transaction_amount&quot;</span>));</span><br><span class="line">        String dateString = fieldSet.readString(<span class="string">&quot;transaction_date&quot;</span>);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            transactionRecord.setTransactionDate(dateFormat.parse(dateString));</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ParseException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> transactionRecord;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="模型"><a href="#模型" class="headerlink" title="模型"></a>模型</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.batch.model;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.xml.bind.annotation.XmlRootElement;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> fengxuechao</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@XmlRootElement(name = &quot;transactionRecord&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Transaction</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> String username;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> userId;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Date transactionDate;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">double</span> amount;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="record-csv"><a href="#record-csv" class="headerlink" title="record.csv"></a>record.csv</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">devendra, 1234, 31&#x2F;10&#x2F;2015, 10000</span><br><span class="line">john    , 2134, 3&#x2F;12&#x2F;2015 , 12321</span><br><span class="line">robin   , 2134, 2&#x2F;02&#x2F;2015 , 23411</span><br></pre></td></tr></table></figure>

<h2 id="启动程序"><a href="#启动程序" class="headerlink" title="启动程序"></a>启动程序</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.littlefxc.examples.batch;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.batch.core.Job;</span><br><span class="line"><span class="keyword">import</span> org.springframework.batch.core.JobExecution;</span><br><span class="line"><span class="keyword">import</span> org.springframework.batch.core.JobParameters;</span><br><span class="line"><span class="keyword">import</span> org.springframework.batch.core.launch.JobLauncher;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.support.ClassPathXmlApplicationContext;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">App</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        ClassPathXmlApplicationContext context = <span class="keyword">new</span> ClassPathXmlApplicationContext();</span><br><span class="line">        context.setConfigLocations(<span class="string">&quot;classpath:spring-context.xml&quot;</span>);</span><br><span class="line">        context.refresh();</span><br><span class="line"></span><br><span class="line">        JobLauncher jobLauncher = (JobLauncher) context.getBean(<span class="string">&quot;jobLauncher&quot;</span>);</span><br><span class="line">        Job job = (Job) context.getBean(<span class="string">&quot;firstBatchJob&quot;</span>);</span><br><span class="line">        System.out.println(<span class="string">&quot;Starting the batch job&quot;</span>);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            JobExecution execution = jobLauncher.run(job, <span class="keyword">new</span> JobParameters());</span><br><span class="line">            System.out.println(<span class="string">&quot;Job Status : &quot;</span> + execution.getStatus());</span><br><span class="line">            System.out.println(<span class="string">&quot;Job completed&quot;</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">            System.out.println(<span class="string">&quot;Job failed&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="验证"><a href="#验证" class="headerlink" title="验证"></a>验证</h2><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">transactionRecord</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">transactionRecord</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">amount</span>&gt;</span>10000.0<span class="tag">&lt;/<span class="name">amount</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">transactionDate</span>&gt;</span>2015-10-31T00:00:00+08:00<span class="tag">&lt;/<span class="name">transactionDate</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">userId</span>&gt;</span>1234<span class="tag">&lt;/<span class="name">userId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">username</span>&gt;</span>devendra<span class="tag">&lt;/<span class="name">username</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">transactionRecord</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">transactionRecord</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">amount</span>&gt;</span>12321.0<span class="tag">&lt;/<span class="name">amount</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">transactionDate</span>&gt;</span>2015-12-03T00:00:00+08:00<span class="tag">&lt;/<span class="name">transactionDate</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">userId</span>&gt;</span>2134<span class="tag">&lt;/<span class="name">userId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">username</span>&gt;</span>john<span class="tag">&lt;/<span class="name">username</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">transactionRecord</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">transactionRecord</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">amount</span>&gt;</span>23411.0<span class="tag">&lt;/<span class="name">amount</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">transactionDate</span>&gt;</span>2015-02-02T00:00:00+08:00<span class="tag">&lt;/<span class="name">transactionDate</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">userId</span>&gt;</span>2134<span class="tag">&lt;/<span class="name">userId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">username</span>&gt;</span>robin<span class="tag">&lt;/<span class="name">username</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">transactionRecord</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">transactionRecord</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="附录："><a href="#附录：" class="headerlink" title="附录："></a>附录：</h2><h3 id="application-properties"><a href="#application-properties" class="headerlink" title="application.properties"></a>application.properties</h3><figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">batch.schema-drop=org/springframework/batch/core/schema-drop-mysql.sql</span></span><br><span class="line"><span class="string">batch.schema-create=org/springframework/batch/core/schema-mysql.sql</span></span><br><span class="line"><span class="string">batch.sql=INSERT</span> <span class="string">INTO</span> <span class="string">transaction_record</span> <span class="string">(user_id,</span> <span class="string">username,</span> <span class="string">transaction_date,</span> <span class="string">amount)</span> <span class="string">VALUES</span> <span class="string">(:userId,</span> <span class="string">:username,</span> <span class="string">:transactionDate,</span> <span class="string">:amount)</span></span><br><span class="line"></span><br><span class="line"><span class="string">jdbc.url=jdbc:mysql://192.168.120.63:3306/batch?useSSL=false</span></span><br><span class="line"><span class="string">jdbc.username=root</span></span><br><span class="line"><span class="string">jdbc.password=123456</span></span><br><span class="line"><span class="string">jdbc.driver-class-name=com.mysql.jdbc.Driver</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 自定义数据库删除脚本</span></span><br><span class="line"><span class="string">project.schema-drop=classpath:schema-drop.sql</span></span><br><span class="line"><span class="comment"># 自定义数据库创建脚本</span></span><br><span class="line"><span class="string">project.schema-create=classpath:schema.sql</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Mybatis Config</span></span><br><span class="line"><span class="string">mybatis.configuration=classpath:mybatis-config.xml</span></span><br><span class="line"><span class="string">mybatis.type-aliases-package=com.littlefxc.examples.batch.model</span></span><br><span class="line"><span class="string">mybatis.mapper.base-package=com.littlefxc.examples.batch.dao</span></span><br></pre></td></tr></table></figure>

<h3 id="mybatis-config-xml"><a href="#mybatis-config-xml" class="headerlink" title="mybatis-config.xml"></a>mybatis-config.xml</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">configuration</span> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">&quot;-//mybatis.org//DTD Config 3.0//EN&quot;</span> <span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">settings</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;cacheEnabled&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;lazyLoadingEnabled&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;multipleResultSetsEnabled&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;useColumnLabel&quot;</span> <span class="attr">value</span>=<span class="string">&quot;true&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;useGeneratedKeys&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;autoMappingBehavior&quot;</span> <span class="attr">value</span>=<span class="string">&quot;PARTIAL&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;autoMappingUnknownColumnBehavior&quot;</span> <span class="attr">value</span>=<span class="string">&quot;WARNING&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;defaultExecutorType&quot;</span> <span class="attr">value</span>=<span class="string">&quot;SIMPLE&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;defaultStatementTimeout&quot;</span> <span class="attr">value</span>=<span class="string">&quot;25&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;defaultFetchSize&quot;</span> <span class="attr">value</span>=<span class="string">&quot;100&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;safeRowBoundsEnabled&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;mapUnderscoreToCamelCase&quot;</span> <span class="attr">value</span>=<span class="string">&quot;false&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;localCacheScope&quot;</span> <span class="attr">value</span>=<span class="string">&quot;SESSION&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;jdbcTypeForNull&quot;</span> <span class="attr">value</span>=<span class="string">&quot;OTHER&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">&quot;lazyLoadTriggerMethods&quot;</span> <span class="attr">value</span>=<span class="string">&quot;equals,clone,hashCode,toString&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">settings</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="schema-sql"><a href="#schema-sql" class="headerlink" title="schema.sql"></a>schema.sql</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> Navicat Premium Data Transfer</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> Source Server         : localhost</span></span><br><span class="line"><span class="comment"> Source Server Type    : MySQL</span></span><br><span class="line"><span class="comment"> Source Server Version : 50722</span></span><br><span class="line"><span class="comment"> Source Host           : localhost:3306</span></span><br><span class="line"><span class="comment"> Source Schema         : batch</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> Target Server Type    : MySQL</span></span><br><span class="line"><span class="comment"> Target Server Version : 50722</span></span><br><span class="line"><span class="comment"> File Encoding         : 65001</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment"> Date: 31/01/2019 10:27:20</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">SET</span> NAMES utf8mb4;</span><br><span class="line"><span class="keyword">SET</span> FOREIGN_KEY_CHECKS <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- ----------------------------</span></span><br><span class="line"><span class="comment">-- Table structure for transaction_record</span></span><br><span class="line"><span class="comment">-- ----------------------------</span></span><br><span class="line"># <span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> `transaction_record`;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `transaction_record`  (</span><br><span class="line">  `username` <span class="type">varchar</span>(<span class="number">255</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8 <span class="keyword">COLLATE</span> utf8_general_ci <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">  `user_id` <span class="type">bigint</span>(<span class="number">20</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">  `transaction_date` datetime(<span class="number">6</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">  `amount` <span class="keyword">double</span>(<span class="number">11</span>, <span class="number">0</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">  <span class="keyword">PRIMARY</span> KEY (`username`) <span class="keyword">USING</span> BTREE</span><br><span class="line">) ENGINE <span class="operator">=</span> InnoDB <span class="type">CHARACTER</span> <span class="keyword">SET</span> <span class="operator">=</span> utf8 <span class="keyword">COLLATE</span> <span class="operator">=</span> utf8_general_ci ROW_FORMAT <span class="operator">=</span> <span class="keyword">Dynamic</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">SET</span> FOREIGN_KEY_CHECKS <span class="operator">=</span> <span class="number">1</span>;</span><br></pre></td></tr></table></figure>

<h3 id="schema-drop-sql"><a href="#schema-drop-sql" class="headerlink" title="schema-drop.sql"></a>schema-drop.sql</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> `transaction_record`;</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2019/05/06/%E8%87%AA%E5%AE%9A%E4%B9%89spring-boot-starter/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2019/05/06/%E8%87%AA%E5%AE%9A%E4%B9%89spring-boot-starter/" class="post-title-link" itemprop="url">自定义spring-boot-starter</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2019-05-06 19:59:31" itemprop="dateCreated datePublished" datetime="2019-05-06T19:59:31+08:00">2019-05-06</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:49" itemprop="dateModified" datetime="2021-03-25T21:15:49+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="自定义Starter-myQuartz-spring-boot-starter"><a href="#自定义Starter-myQuartz-spring-boot-starter" class="headerlink" title="自定义Starter:myQuartz-spring-boot-starter"></a>自定义Starter:myQuartz-spring-boot-starter</h1><p>模仿学习Spring Boot starter写一个关于Quartz的自动配置的依赖。</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2019/05/06/%E8%87%AA%E5%AE%9A%E4%B9%89spring-boot-starter/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/21/"><i class="fa fa-angle-left" aria-label="上一页"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/21/">21</a><span class="page-number current">22</span><a class="page-number" href="/page/23/">23</a><a class="page-number" href="/page/24/">24</a><a class="extend next" rel="next" href="/page/23/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>


<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>






  





</body>
</html>
